14

Say I've typed the following long command into the command line

 $ some very long command that I want to copy to the clipboard

How can I get this onto the system clipboard efficiently? I'm using Gnome Terminal 2.23.1.

I know that I can get the mouse and highlight it and copy it, but I want a general keyboard solution.

It would also be good if it generalised to multiple command-line programs.

For example, in bash in vi mode, I can press v and then edit the command in vim, and thus use Vim's copy to clipboard features, but this doesn't work when using R on the command-line. It's also a bit slow.

I'd like it if in vi-mode, I could press yy and copy the contents of the line to the system clipboard, but this doesn't seem to work. Furthermore any yanking commands don't seem to copy contents to the system clipboard; they appear to only copy to a buffer used by bash.

UPDATE:

After posting I found

  • 1
    Not exactly what you ask: using screen you can achieve what you ask following this answer: askubuntu.com/questions/56570/… – enzotib Oct 5 '11 at 8:04
  • Following this answer ( askubuntu.com/a/503861/43594 ), you can enable Mouse Keys in Universal Access and use the number keypad to move the mouse around, with the number five acting as a click. When it's quickly double clicked it will select the word the pointer is hovering over. When it's quickly triple clicked, it will select the entire line. This along with the answers on how to copy (Ctrl-Shift-C) should do most of what you need. – James Jul 28 '14 at 22:22
3

You could also use a clipboard manager like parcellite. It's a GTK+ clipboard manager which also has command-line clipboard capabilities

You can use it via command-line by

echo "some very long command that I want to copy to the clipboard" | parcellite
##or##
parcellite "some very long command that I want to copy to the clipboard"

It is not installed by default and so you'll have to install parcellite by sudo apt-get install parcellite

  • 1
    thanks, at least when I'm in bash, I've sometimes used echo my command | xclip -se c; however, when the command has quotations and other special characters, echo often wont copy the command literally. – Jeromy Anglim Oct 5 '11 at 8:51
1

So the closes I have be able to get is by using the history and xclip in conjunction doing this

echo !! | xclip

This will put the last command on the the center button clipboard. I am still searching for something better though as I would like to get it down to one command. However I get hung up because history doesn't function in shell scripts and I don't know that there is a way to use !! properly in an alias. Of course this doesn't appear to be quite what your asking for my intention is to use this for taking notes of what I am doing. Any thoughs?

  • Also, there's still the issue of how do you copy commands with quotes. – Jeromy Anglim Oct 16 '11 at 0:57
0

You should be able to use Ctrl+Shift+C for Copy and Ctrl+Shift+V for Paste anywhere in the Terminal.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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