Ubuntu – Terminal – Selecting commands I’ve entered using the keyboard

command lineshortcut-keys

I can highlight text I've entered in the terminal with my mouse and then use ctrl + shift + c to copy to the clipboard and that was fine for a while. But I've tried highlighting text by pressing shift and ctrl + shift like you can do in text editors. Neither seems to work. Having a keyboard shortcut for copying terminal commands I've entered would be much easier than dragging the mouse everytime I want to copy something. Is there any way to do that using the keyboard? I've tried using ctrl + u followed by ctrl + y, but that doesn't copy text to the clipboard, so I can't use that anywhere but the terminal.

Best Answer

There is a set of shortcuts for terminal , and they are organized around the current cursor position.

  • You can use CtrlK shortcut to cut the text from cursor to end of line
  • CtrlU cuts from current position to beginning of line.
  • Paste with CtrlY

These two are pretty useful in particular when you want to either copy the command or its arguments.

If you are proficient with vim text editor, you can edit the command you want in a more powerful way by evoking vim with fc command.

For using the command outside the terminal, you might want to use xclip command (not installed by default) . For instance,

$ echo "some_command" | xclip -sel clip

Once you have xclip you can add the following function to your .bashrc file

to_clipboard() {
    xclip -sel clip <<<"$@" 
} 

What this does is it will copy whatever you put in front to clip board. You can use that in combination with the shortcuts above to cut test, paste it in front of the function, and it will be added to your clipboard. Small example

$ to_clipboard echo 'hello world'