10

I was recently thrown there from the windows world and I'd like to ask some questions about clipboards in Linux (and Unix?) systems. What is X clipboard? How many clipboards are in Linux? Actually, I'd like to copy command from terminal to clipboard and then paste it in an other application (gedit, skype etc.). I entered echo !! | xclip in terminal. However when I tried to paste the command to other applications (via Shift+Ins or Ctrl+V), actually an old content have been pasted. At the same time I can paste this command in terminal.

0

3 Answers 3

12

The xclip manpages (man xclip) say this:

 -selection
      specify which X selection to use, options are "primary" to use XA_PRIMARY
      (default), "secondary" for XA_SECONDARY or "clipboard" for XA_CLIPBOARD

So you can specify the location:

echo !! | xclip -selection <selection>

Where <selection> is one of primary, secondary, clipboard.

Description of these from the ArchWiki: Clipboard

Of the three selections, users should only be concerned with PRIMARY and CLIPBOARD. SECONDARY is only used inconsistently and was intended as an alternate to PRIMARY. Different applications may treat PRIMARY and CLIPBOARD differently; however, there is a degree of consensus that CLIPBOARD should be used for Windows-style clipboard operations, while PRIMARY should exist as a "quick" option, where text can be selected using the mouse or keyboard, then pasted using the middle mouse button (or some emulation of it). This can cause confusion and, in some cases, inconsistent or undesirable results from rogue applications.

That means it depends on your environment. It may have inconsistencies, if the applications use different selections. Though if you use a desktop enivornment like Gnome it should work fine.

Also as mentioned by Kartik, you can copy-paste in most terminals with Ctrl+Shift+C/V.

Most applications in linux also support selection of text with the mouse to store something in the clipboard.

To get the content of from the clipboard use the -o flag:

xclip -o

If you use it often you can create aliases for those commands in you .bashrc:

alias cbcopy='xclip -selection clipboard'
alias cbpaste='xclip -selection clipboard -o'
3

For the record, in Cygwin you can direct your output to /dev/clipboard like so:

echo !! > /dev/clipboard

This copies the output to the system standard clipboard and you can use Ctrl+V or whatever to paste it.

2
  • 3
    But that's only for Cygwin, isn't it?
    – ahilsend
    Jul 24, 2013 at 15:09
  • Oh, how embarassing! I didn't know that. Yeah, I have to use Cygwin, unfortunately. I'll update the answer. Thanks for the info Jul 24, 2013 at 15:23
2

To copy anything in linux from the terminal, there are two ways:

  1. Select it with the mouse to copy and paste it with middle click.
  2. Select with mouse and copy it with the keyboard shortcut. (which is different on different terminals, Ctrl-Shift-C/V on Ubuntu)

Sometimes you can copy and paste anything with Ctrl+C/V like windows (but not on the terminals).

EDIT: echo !! | xclip copies only the previous command.

8
  • Thank you. So, there in no way to copy in Linux from terminal without mouse?
    – Loom
    Jul 24, 2013 at 12:38
  • 1
    @Loom I think you can copy with echo "something" | xcopy and paste with Shift + Ins
    – Kartik
    Jul 24, 2013 at 12:42
  • Thanks. What xcopy is and where I can find it?
    – Loom
    Jul 24, 2013 at 12:48
  • @Loom Maybe outside terminal you can paste with Ctrl+V
    – Kartik
    Jul 24, 2013 at 12:49
  • @Loom I'm sorry i meant to say xclip
    – Kartik
    Jul 24, 2013 at 12:51

Not the answer you're looking for? Browse other questions tagged .