97

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
kmario23
  • 1,019
  • 2
  • 10
  • 20
Strapakowsky
  • 11,914
  • 1
    You mean Ctrl-Shift-Home? – Mechanical snail May 31 '13 at 06: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 06:19
  • 1
    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 04 '18 at 01:47

7 Answers7

36

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.

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.

Line
  • 351
  • 1
  • 4
  • 15
ElderDelp
  • 558
  • 3
  • 9
19

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.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
Nykakin
  • 3,744
16

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.

Carolus
  • 587
demure
  • 2,282
  • 1
  • 16
  • 19
11

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

Meer Borg
  • 4,963
2

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" 
Kris
  • 131
  • 7
1

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

0

Assuming bash or compatible, you can copy the command from your history to the clipboard after running it:

$ fc -lrn | head -1 | xclip -se c

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):

$ c2f() { fc -lrn | head -1 >> "${1?}"; }

Usage:

$ cat <(echo '!! example command that "includes chars" with shell > semantics, etc') | cat >&2
$ c2f readme.md

readme.md now contains:

...
cat <(echo '!! example command that "includes chars" with shell > semantics, etc') | cat >&2

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...```.

Roger Dahl
  • 2,192