Terminal - copy from CLI without using mouse
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.
Related videos on Youtube
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, 2022Comments
-
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.
-
ott-- over 10 yearsYou're using gnome without a mouse?
-
-
prayagupa over 10 yearsThere was no
screen
command, so I installed it in gnome-terminal and startedscreen
in a terminal. But couldn't get started thereafter. Also I didn't understand what you mean by built-in screen. -
dawud over 10 yearsRead
man screen
. Also, I wrote 'built-in in screen', meaning the copy/paste functionality is present inscreen
by design . -
prayagupa over 10 yearsThat's ok, but I still have to use mouse?
-
terdon over 10 years@PrayagUpd ah, yes, that will copy to the middle click buffer. See updated answer for another way.
-
prayagupa over 10 yearsI think
$ xclip -o
is not what I want because it just prints what Ixclipe
d. But +1 forCtrl+A
followed byCtrl+K
andCtrl+Y
for cutting and pasting to the at least in the same terminal. -
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 over 10 yearsGreat
Ctrl+K
and thenCtrl+Y
works withinmysql>
as well but I can't paste the same thing somewhere else neither in normal nor inmysql
mode. -
terdon over 10 years@PrayagUpd see updated answer for another trick.
-
mikeserv over 9 yearsThis is not robust.
-
mikeserv over 9 yearsI 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 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 assumedbash
andlinux
. Moreover, assumingbash
is not a big deal because the script would still work find in zsh or most other shells. -
mikeserv over 9 yearsI 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 almost 9 yearsYou need to use
space
key to select desirable area. You need to useY
to copy whole line or desirable area. Screen isvi
movements friendly. -
alhelal almost 6 yearswhat is
AltGr
key? I haveAlt
-
alhelal almost 6 yearsI used
Ctrl + Alt + [
but not works. -
Luís de Sousa over 4 yearsThe
screen
tool does not function has explained here. And it does not seem to provide an answer to the question.