Terminal - copy from CLI without using mouse

35,058

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
35,058

Related videos on Youtube

prayagupa
Author by

prayagupa

(def summary[] (:TCP/IP-socket-programmer "who loves to send and receive bits and bytes") (:using "bytecode instructions which runs on JVM [clojure, groovy, java12]") (and ([sql, nosql])) (also (did [PHP, CLR] socket programming once upon a time)) (does mobile app programming sometimes in [Android] Platform.) (TDD practitioner)) (def resume[] {:stackoverflow_careers "http://careers.stackoverflow.com/prayagupd" ))

Updated on September 18, 2022

Comments

  • prayagupa
    prayagupa over 1 year

    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

    • ott--
      ott-- over 10 years
      You're using gnome without a mouse?
  • prayagupa
    prayagupa over 10 years
    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.
  • dawud
    dawud over 10 years
    Read man screen. Also, I wrote 'built-in in screen', meaning the copy/paste functionality is present in screen by design .
  • prayagupa
    prayagupa over 10 years
    That's ok, but I still have to use mouse?
  • terdon
    terdon over 10 years
    @PrayagUpd ah, yes, that will copy to the middle click buffer. See updated answer for another way.
  • prayagupa
    prayagupa over 10 years
    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.
  • terdon
    terdon over 10 years
    @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.
  • prayagupa
    prayagupa over 10 years
    Great Ctrl+K and then Ctrl+Y works within mysql> as well but I can't paste the same thing somewhere else neither in normal nor in mysql mode.
  • terdon
    terdon over 10 years
    @PrayagUpd see updated answer for another trick.
  • mikeserv
    mikeserv over 9 years
    This is not robust.
  • mikeserv
    mikeserv over 9 years
    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
  • shivams
    shivams over 9 years
    @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.
  • mikeserv
    mikeserv over 9 years
    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.
  • FelikZ
    FelikZ almost 9 years
    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.
  • alhelal
    alhelal almost 6 years
    what is AltGr key? I have Alt
  • alhelal
    alhelal almost 6 years
    I used Ctrl + Alt + [ but not works.
  • Luís de Sousa
    Luís de Sousa over 4 years
    The screen tool does not function has explained here. And it does not seem to provide an answer to the question.