Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
    
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

4 Answers 4

up vote 2 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.

share|improve this answer

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.

share|improve this answer
    
I knew that but want to paste outside the terminal. Edited for clarification. –  Strapakowsky May 31 '13 at 5:09

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

share|improve this answer
    
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

The closest I can think of is ctrl+u, ctrl+y
This would delete from curse to 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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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