96

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
3
  • 1
    You mean Ctrl-Shift-Home? May 31, 2013 at 6:14
  • Shift-Home copies to the beginning of the line. Ctrl-Shift-Home copies to the beginning of the page. May 31, 2013 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, 2018 at 1:47

7 Answers 7

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.

4
  • 3
    I knew that but want to paste outside the terminal. Edited for clarification. May 31, 2013 at 5:09
  • 12
    control-U can cut the command from the end of it directly
    – vstepaniuk
    Feb 14, 2018 at 18:47
  • 2
    2nd command changed my life.
    – Krishna
    Jul 31, 2019 at 6:44
  • @BalKrishnaJha Even better than Ctrl+U -- You can insert arguments from your previous commands into the current one with Alt+. (press multiple times to grab older arguments).
    – Roger Dahl
    Jul 12, 2021 at 1:57
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.

7
  • 3
    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, 2016 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, 2016 at 9:59
  • 4
    Any explanation as to what's actually going on here?
    – Alexander
    Mar 22, 2018 at 17:06
  • 1
    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, 2018 at 18:49
  • 1
    @Alexander It sets up a keybinding, that when you press it, inputs the current line into xsel, which is a clipboard manager.
    – wjandrea
    Aug 24, 2018 at 18:51
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.

1
  • 2
    I knew that but want to paste outside the terminal. Edited for clarification. May 31, 2013 at 5:10
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

3
  • 2
    Good suggestion, but I don't like screen for different reasons, so assume regular Ubuntu terminal. Edited my question for clarification. May 31, 2013 at 5:11
  • 1
    This should be marked as the correct answer IMHO.
    – eddiewould
    Jan 19, 2016 at 7:15
  • @Strapakowsky what are those different reasons to ask a few if I may ask
    – FantomX1
    May 12, 2020 at 0:52
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" 
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...```.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .