Keyboard shortcut to copy current command from terminal to clipboard
9,921
Yes, first you have to install xclip.
(sudo apt-get install xclip
)
Then to copy previous line:
echo !! | xclip
To Paste: Middle click or
xclip -o
For example:
date
Wed Jul 24 15:46:54 IST 2013
echo !! | xclip
xclip -o
date
To copy Output of any command: <command> | xclip
However, this may fail if the command has special characters.
Related videos on Youtube
Author by
Loom
Updated on September 18, 2022Comments
-
Loom over 1 year
Is there any way to copy current command (line typed in bash prompt) in terminal to X-clipboard without involving a mouse?
-
Marco almost 11 yearsWhat is the “current command”? If you know what to copy, you can use xclip.
-
Loom almost 11 years@Marco - Thank you - I expanded question. As for xclip, I am a newbie and do not know what it about. Could you explain?
-
Loom almost 11 years@Gilles - Frankly, I do not really understand how the knowledge of how to put the selected text to the clipboard can help with putting the current command to the clipboard. Is there a keyboard shortcut, that allows to select the current command from bash prompt?
-
-
Gilles 'SO- stop being evil' almost 11 yearsThat fails if the command contains just about any special character.
-
Hielke Walinga over 5 yearsJust add
:q
to!!
of your command and it is fixed:echo !!:q | xclip.
:q
makes sure your variable is expanded and only then quoted. (Single quotes, so that nothing is interpreted by the shell.)