While firing commands in CLI, I want to copy the command I just fired or anything I wrote in terminal and paste it somewhere else without using mouse.

Like in following picture, I want to copy the update command (completely or partially) without using mouse and paste it somewhere.

copy from CLI without using mouse

  • 1
    You're using gnome without a mouse?
    – ott--
    Commented Sep 22, 2013 at 19:37

7 Answers 7


You can use screen(1) within your terminal emulator of choice (xterm, gnome-terminal, ...).

The functionality you need is built-in in screen. You need to get familiar with the way it works:

  • by default, the "command" keybinding is Ctrl + A, you compose commands by issuing the "command" sequence plus the specific action.
  • the "copy" command is: AltGr + [
  • the "paste" command is AltGr + ]

You can "copy" the command and "paste" in another terminal.

screen is fun and once you start using it you'll wonder how it is possible you haven't been using it at all.

  • There was no screen command, so I installed it in gnome-terminal and started screen in a terminal. But couldn't get started thereafter. Also I didn't understand what you mean by built-in screen. Commented Sep 22, 2013 at 8:51
  • 1
    Read man screen. Also, I wrote 'built-in in screen', meaning the copy/paste functionality is present in screen by design .
    – dawud
    Commented Sep 22, 2013 at 8:55
  • You need to use space key to select desirable area. You need to use Y to copy whole line or desirable area. Screen is vi movements friendly.
    – FelikZ
    Commented May 18, 2015 at 12:39
  • 1
    what is AltGr key? I have Alt
    – alhelal
    Commented May 25, 2018 at 3:00
  • 1
    The screen tool does not function has explained here. And it does not seem to provide an answer to the question. Commented Aug 8, 2019 at 10:05

In addition to the answers already provided, you could consider moving to a more flexible terminal emulator.

If you were prepared to change terminals to rxvt-unicode, you could use a tool like urxvt-perls: a collection of scripts that provide the following keyboard functionality in that terminal emulator:

  • select, copy and paste text
  • forward and reverse search
  • highlight and open URLs in your preferred browser

You simply install the package and then add a few lines to your ~/.Xresources, and you can then unplug the rodent for good.


You can do this using a program like xclip:

   xclip - command line interface to X selections (clip‐

Once you have installed it, you can use it to connect to your X clipboard. Unfortunately, this won't work in your mysql environment (it has its own buffer for copied lines) but it will if you want to run a 'normal' command. For example :

$ This is a long command line

Type CtrlA to go to the beginning of the line, enclose the command in quotes then echo it:

$ echo -e "This is a long command line" |xclip 

You now have "This is a long command line" in your middle click clipboard.

If you want to paste without using a mouse, it will depend on where you are pasting. You can paste into another terminal by running:

$ xclip -o

You can paste into the same terminal, simply by killing (cutting) the command with CtrlK and then pasting with CtrlY.

You can also save commands across terminals using bash's history. Add this line to your ~/.bashrc:

PROMPT_COMMAND='history -a; history -r'

PROMPT_COMMAND is a special bash variable. If it is set the value is executed as a command before issuing a new prompt. history -a will write the history of the current session to the history file and history -r will reload that file. This means that every command you run will be immediately written to the history file.

Now when you run a long command line, you can switch to another terminal and hit return (just to run $PROMT_COMMAND, alternatively, open a new terminal window) and it will be accessible to this terminal's history. If you now hit Up you can run it on the new terminal.

  • That's ok, but I still have to use mouse? Commented Sep 22, 2013 at 17:10
  • @PrayagUpd ah, yes, that will copy to the middle click buffer. See updated answer for another way.
    – terdon
    Commented Sep 22, 2013 at 17:22
  • I think $ xclip -o is not what I want because it just prints what I xcliped. But +1 for Ctrl+A followed by Ctrl+K and Ctrl+Y for cutting and pasting to the at least in the same terminal. Commented Sep 22, 2013 at 18:01
  • @PrayagUpd the thing is there are various clipboard buffers at play and the solution will depend on what exactly you want to do. Could you update your question with a specific example of what you need? I mean copying from where (mysql will be tricky) and to where.
    – terdon
    Commented Sep 22, 2013 at 18:03
  • 1
    @PrayagUpd see updated answer for another trick.
    – terdon
    Commented Sep 23, 2013 at 8:38

@evilsoup has suggested a good solution but it breaks often.

A Quick Solution

Here is a solution that never breaks.

history | tail -2 | head -1 | xclip -selection clipboard

Just run this command and it will copy the command you just ran to the clipboard.

Basically, what it does is that it prints your command history and takes the second last command and feeds it to your clipboard (the last command is this itself, so it selects the second last command).

A Custom Function

Developing on the quick solution,here is a small function I wrote which is pretty smart and will copy the last used command. You can also provide numerical arguments to copy the last-nth command. E.g. for copying the second last command provide 2 as argument

myclipcopy () {

  if [ -z $1 ]
    then  # if no argument was provided then just copy the last used command
    history | tail -2 | head -1 | sed -re 's/[[:space:]]+[[:digit:]]+[[:space:]]+//g' | xclip -selection clipboard
    echo 'Anyways, the following command has been copied:'
    history | tail -2 | head -1| sed -re 's/[[:space:]]+[[:digit:]]+[[:space:]]+//g'

    myindex=$(( $1+1 ))
    history | tail -$myindex | head -1 | sed -re 's/[[:space:]]+[[:digit:]]+[[:space:]]+//g' | xclip -selection clipboard
    echo "The following command has been copied:"
    history | tail -$myindex | head -1 | sed -re 's/[[:space:]]+[[:digit:]]+[[:space:]]+//g'

  #Delete this command itself from the history
  myhisnum=$(history | tail -1 | grep -oP '\s\d+\s' | grep -oP '\d+'); history -d $myhisnum

Copy-paste the above function in your ~/.bashrc file. Note that if you put this in a separate bash script file, and run the script, it wouldn't work as the history command would not be able to access your history in that case.

Finally, run it as:

myclipcopy 3
# This copies the third-last command

Moreover, one cool feature added to this function is that it deletes itself from the history after it is run. This is helpful because then the "backward" index of the commands doesn't change.

  • This is not robust.
    – mikeserv
    Commented Jan 23, 2015 at 21:39
  • @mikeserv: I have renamed it now to "Custom Function". But why did you downvote it? Don't you notice that this properly answers the question? Moreover, why I said "robust" was because it actually should never break. By downvoting, you are just hiding an appropriate answer from the post.
    – shivams
    Commented Jan 24, 2015 at 4:44
  • I downvoted because you assume a lot: particularly bash and linux, neither of which are specified in the question. Also, your pipeline is ridiculously overcomplicated, and the same is true of your regex
    – mikeserv
    Commented Jan 24, 2015 at 15:31
  • @mikeserv: Thanks for the comment. Hopefully this will help me produce better answers. I will accept that my regex and pipelines are ugly. But they are well tested and I have been using it. And as for assumptions, did you notice that the highest rated answer had taken the assumption that the user was using screen. The second highest rated answer had also assumed bash and linux. Moreover, assuming bash is not a big deal because the script would still work find in zsh or most other shells.
    – shivams
    Commented Jan 24, 2015 at 16:34
  • I didn't say it was ugly - I said it was overcomplicated. And no, it won't work fine in most other shells. And what's more, the CLI actually pictured in the question is MySQL. Good point about the other answers, though.
    – mikeserv
    Commented Jan 24, 2015 at 16:50

This command uses bash's history expansion (so !! expands into the last line you typed into the terminal). You will probably have to install either xclip or xsel.

echo "!!" | xclip -selection clipboard

echo "!!" | xsel -i --clipboard

This will put your last command in the Ctrl+v clipboard, so you will be able to paste in into any GUI program (or with Ctrl+Shift+v in most terminal emulators, or with "+p in vim).

This will break for some lines containing double quotes, for example:

$ echo "foo;bar"
$ echo "!!"
echo "echo "foo;bar""
echo foo
The program 'bar' is currently not installed. You can install it by typing:
sudo apt-get install bar

It will work for the command you gave as an example though; you should only have problems if the double-quotes are escaping ; & && | || and so on.

  • Thanks for this answer, this was the most practical solution I could immediately implement. And I believe the issues with " can be avoided simply by not quoting the !! at all, just do echo !! and it will work flawlessly (except in the improbable case that your previous command began with -n or -e or the few other options that echo itself takes).
    – Sundar R
    Commented Aug 21, 2015 at 16:47

Maybe not the most convenient solution but I use it and so it can be helpful for others more it doesn't require any additional soft installation.

  1. You need to create a temporary file at any convenient directory

  2. Transfer required direction or text to that file. For transferring direction you can use

    echo $(pwd) 1> tempo
  3. To get direction from the file use command

    cd $(tail -n 1 tempo)

For copying and pasting commands this system will work either of course. Hope that was helpful

$ emacs -g '80x24' --eval '(term "/bin/bash")'
C-c C-k    char mode
C-c C-j    line mode
C-space    Selecting text in terminal without using the mouse
M-w        copy to X11 clipboard

where C = Ctrl and M = Alt.

You must log in to answer this question.

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