I am about to press enter to run a command in terminal, but before doing that, I want to copy the command to clipboard without using the mouse.

How?

If you're somewhere other than the terminal, Ctrl+Home does it.

Is there a way of arbitrarily selecting text like that in the terminal?

EDITED:

  • assume that using other programs like screen is not a good alternative
  • the text is to be pasted outside the terminal, so Ctrl+y and similar sequences do not solve it either
  • 1
    You mean Ctrl-Shift-Home? – Mechanical snail May 31 '13 at 6:14
  • Shift-Home copies to the beginning of the line. Ctrl-Shift-Home copies to the beginning of the page. – Strapakowsky May 31 '13 at 6:19
  • Shift-Home and Ctrl-Shift-Home do the same for me (jump to beggining of terminal) and neither of them seem to copy anything to be pased by neither Ctrl-Y or Ctrl-Shift-V (nor select). But it does work in text editor, not copying, just selecting. Using OpenSUSE Leap 15 in Virtual Box. Every other key-binding mentioned in answers works. – Brambor Nov 4 at 1:47
up vote 14 down vote accepted

Bind following shortcut:

bind '"\C-p": "\C-e\C-u xsel <<"EOF"\n\C-y\nEOF\n\C-y"'

Now after using Crtl+P your line will be copied into clipboard. You can paste it in terminal using:

xsel

And into any X application using middle mouse button or Shift+Insert.

  • i want to avoid using the mouse. Is it possible to paste it with ctrl+shift+v instead of middle mouse button? – MrGigu Aug 19 '16 at 9:24
  • @MrGigu, this uses a different clipboard (see here). You need to use other shortcut. By default it should be Shift+Insert (see here) – Nykakin Aug 19 '16 at 9:59
  • Oh thanks, shift+insert works will do! didnt know you could paste with this command. – MrGigu Aug 23 '16 at 14:29
  • Any explanation as to what's actually going on here? – Alexander Mar 22 at 17:06
  • Or use xsel -b to use the standard clipboard, i.e the one that uses Ctrl+V/Ctrl+Shift+V. Also worth noting that this method fails on multi-line commands. – wjandrea Aug 24 at 18:49

If you are using one of the shells that understands emacs keys (bash, csh, etc.) then you can copy the current command by:

  1. control-A will take you to the beginning of the line.

  2. control-K will kill the whole line that you have just entered.

  3. control-Y will yank the text back.

Then later you can control-Y yank the text back to insert the text back as input to the shell command line editor.

See man bash and then when it comes up, type /emacs followed by a couple of n's (next) to move you forward to the READLINE section.

  • I knew that but want to paste outside the terminal. Edited for clarification. – Strapakowsky May 31 '13 at 5:09
  • 2
    control-U can cut the command from the end of it directly – vstepaniuk Feb 14 at 18:47

The closest I can think of is Ctrl+u, Ctrl+y
This would delete from cursor to the beginning of line, then paste from the readline buffer. This isn't exactly the same as the clipboard though, but you would be able to paste inside the shell, if that is what you need.

  • I knew that but want to paste outside the terminal. Edited for clarification. – Strapakowsky May 31 '13 at 5:10

There is a program called screen. It creates a text windowing system that allows you to switch between multiple instances. But it also allows you to select text.

sudo apt-get install screen

That command installs it.

Then type screen

You use ctr-a to start the command sequence. Then press esc and your cursor will move in any direction. Press enter to start text selection, move to end point, press enter again. That will copy to buffer.

Then ctr-a and then } will paste

More details about other commands here http://www.kuro5hin.org/story/2004/3/9/16838/14935

  • Good suggestion, but I don't like screen for different reasons, so assume regular Ubuntu terminal. Edited my question for clarification. – Strapakowsky May 31 '13 at 5:11
  • 1
    This should be marked as the correct answer IMHO. – eddiewould Jan 19 '16 at 7:15

If you are inside vim you can visually select one or more lines with Shift+v and then use a binding, e.g. yy, to pipe the selection to xclip.

Add the binding to your vimrc:

vnoremap yy :w !xclip -selection clipboard<CR><CR>

This requires xclip to be installed, it is in the Debian/Ubuntu aptitude repository.

xclip stores stdin, with the -selection clipboard option it also pushes stdin to the system clipboard.

So you can also use xclip in a generic way from the terminal, for example to copy an entire file to the system clipboard:

cat myfile | xclip -selection clipboard

If you can optionally also create an alias, such as:

alias cb="xclip -selection clipboard" 

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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