Is there any way to copy current command (line typed in bash prompt) in terminal to X-clipboard without involving a mouse?
Asked
Active
Viewed 1.0k times
1 Answers
7
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.

Kartik
- 2,004
-
5That fails if the command contains just about any special character. – Gilles 'SO- stop being evil' Jul 24 '13 at 22:02
-
Just 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.) – Hielke Walinga Sep 28 '18 at 16:49
copy_line_to_x_clipboard
function in my answer differ from what you're asking? If you're asking for a shortcut out of the box, then no, it doesn't exist, that's why I wrote a function. You can bind this function to whatever key you like (I also show how to do this in my answer). – Gilles 'SO- stop being evil' Jul 25 '13 at 11:27