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
If you are using one of the shells that understands emacs keys (bash, csh, etc.) then you can copy the current command by:
control-A
will take you to the beginning of the line.control-K
will kill the whole line that you have just entered.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 ofn
's (next) to move you forward to the READLINE section.Bind following shortcut:
Now after using Crtl+P your line will be copied into clipboard. You can paste it in terminal using:
And into any X application using middle mouse button or Shift+Insert.
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.
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.
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
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:
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:If you can optionally also create an alias, such as:
Daniel Micay's Termite sports a "selection mode". Pressing Ctrl+Shift+Space will activate it. It's got vim-like key bindings. v or V will select à la vim's visual mode, y will yank, Esc will exit selection mode.
Copied from https://stackoverflow.com/questions/1536757/selecting-text-in-terminal-without-using-the-mouse/29386401
Assuming
bash
or compatible, you can copy the command from your history to the clipboard after running it:If you don't want to run it before copying it to the clipboard, jump to the front with Ctrl+A and temporarily turn it into a comment by adding
#
.Having an alias for the command line above is handy when recording command line procedures for later reference or documentation. However, it's even more convenient to just append the last command directly to a file as needed by making a command like this (
c2f
=command to file
):Usage:
readme.md
now contains:To make it quicker to edit the file later, it can also be convenient to slightly expand on the above and add some appropriate syntax wrapping the command being append to the file, such as
```bash...```
.