31

Is there a way to copy the currently written content of the bash prompt? Say I wrote:

ping www.google.com

so that the lines in terminal looks like:

petr@sova:~$ ping www.google.com

and didn't hit return yet. I want to copy that text to clipboard. Is that possible?

The usual usecase would be hitting up several times and finding a command which I want to copy.

40

To cut, press ctrl+u. To paste, use ctrl+y. This copies whole line to bash clipboard. If you're using X and default Ubuntu terminal, you can use your mouse to mark contents and press ctrl+shift+c to copy, and ctrl+shift+v to paste.

| improve this answer | |
  • 9
    I am using Xubuntu and while ctrl+u and ctrl+y works in the terminal, I need to be able to cut/copy from terminal and paste in a different app, which doesn't work. Possibly, ctrl+u cuts the text into terminal's own clipboard, not the global one? – mreq Jan 31 '14 at 9:19
  • You have to use X's clipboard. Try ctrl+shift+c and ctrl+shift+v. If you're using terminal (without graphical UI), you're out of luck. If you want to append commands to script file, you can use echo ping www.google.com >> filename.txt to work around it. – Olli Jan 31 '14 at 9:21
  • And tip: you can prefix commands easily by pressing ctrl+u, writing echo and then hitting ctrl+y to paste original command back. – Olli Jan 31 '14 at 9:22
  • @olli, easier would be to press ctrl+a to go to the beginning of prompt – Chicken Soup Feb 22 '19 at 7:45
  • @ratskin I don't know which terminal (emulator) you are using, that doesn't work for me. – Olli Feb 23 '19 at 8:32
4
  • Add a # to the front of the command (so it becomes a comment)
  • Run it
  • Grab it from the history and pipe it to a clipboard utillity like xclip: history | tail -n 1 | sed "s/[[:digit:]]* //" | sed "s/^#//" | xclip
| improve this answer | |
  • 3
    An easier alternative is to add echo in front of the command and pipe to xclip. $ echo <command> | xclip -selection clipboard – Sameer May 2 '19 at 14:46
1

If you don't mind using the mouse, just triple click on the line you want to copy then press Ctrl+Shift+C. You can then paste it with Ctrl+V.

| improve this answer | |
  • 2
    should be ctrl+shift+c to copy, shouldn't it? – Info5ek Apr 23 '17 at 8:50
0

In xfce4-terminal, you need to select with the mouse the text you'd like to copy, then right-click and then select the Copy item in the context-menu.

Does this work?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.