20

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-- Sep 22 '13 at 19:37
11

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. – prayagupd Sep 22 '13 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 Sep 22 '13 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 May 18 '15 at 12:39
  • what is AltGr key? I have Alt – alhelal May 25 '18 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. – Luís de Sousa Aug 8 at 10:05
7

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.

4

You can do this using a program like xclip:

NAME
   xclip - command line interface to X selections (clip‐
   board)

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? – prayagupd Sep 22 '13 at 17:10
  • @PrayagUpd ah, yes, that will copy to the middle click buffer. See updated answer for another way. – terdon Sep 22 '13 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. – prayagupd Sep 22 '13 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 Sep 22 '13 at 18:03
  • 1
    @PrayagUpd see updated answer for another trick. – terdon Sep 23 '13 at 8:38
1

@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'

  else
    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'
  fi

  #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 Jan 23 '15 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 Jan 24 '15 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 Jan 24 '15 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 Jan 24 '15 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 Jan 24 '15 at 16:50
0

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"
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 - Reinstate Monica Aug 21 '15 at 16:47
0

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

  • Kusalananda thanks for editing. Now the answer looks much better – Dmitriy Kormulev Feb 11 '17 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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