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.

Loom
  • 3,953

3 Answers3

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'
ahilsend
  • 1,464
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

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.

Kartik
  • 2,004