3

The xclip manual says that there are three storage locations named primary (XA_PRIMARY), secondary (XA_SECONDARY) and clipboard (XA_CLIPBOARD). I copied (Ctrl+c) some text from my browser and examined the contents of the three locations mentioned above in the terminal:

xclip -s primary -o
xclip -s secondary -o
xclip -s clipboard -o

All three contain the same text.

If I now type

date | xclip -selection XA_PRIMARY

Only the XA_PRIMARY should contain the date text. But in fact the other two i.e. XA_SECONDARY, and XA_CLIPBOARD also contain the same text (date information). So are the three one and the same? And if so what is the point?

What baffled me even more, Ctrl+Shift+v (still in terminal mode) pastes the orgiginal text that I copied from the browser! which suggests that shell's own clipboard is in operation as well.

Does someone have an example that demonstarates that the three locations can hold different values at the same time and how they relate to the regular clipboard?

2

Unfortunately xclip silently ignores invalid command line options:

There is no -s option as both -selection and -silent start with -s. You have to use at least -se.

As xclip just ignores your -s option the commands

xclip -s primary -o
xclip -s secondary -o
xclip -s clipboard -o
xclip -s asdfghj -o

all just use the default selection (primary).

For the selection name only the first character is actually used, and if it is anything but s or c the default is used.

date | xclip -selection XA_PRIMARY
date | xclip -selection XA_SECONDARY
date | xclip -selection XA_CLIPBOARD

all use the primary selection.

Use something like

date | xclip -se p    # write to primary selection
xclip -se c -o        # read from clipboard
  • You are quite right, thank you very much for that. So how can we assign and access the parameters XA_PRIMARY, XA_SECONDARY, XA_CLIPBOARD in command line or in a bash script? – elmclose Oct 21 '16 at 14:29
  • Use primary (or just p) , secondary (or just s) and clipboard (or just c) instead of XA_PRIMARY, XA_SECONDARY, XA_CLIPBOARD. See the two examples at the end of my answer. – Florian Diesch Oct 21 '16 at 14:41
  • Thanks again. So these uppercase parameters are redundant and misleading. I wonder if the authors know about it. – elmclose Oct 21 '16 at 15:03
1

I'll address the missing point, that is "why 3 different clipboards".

  1. Historically, you could issue a command with 1 argument or with 2. First would operate on PRIMARY only, 2nd - on both PRIMARY and SECONDARY. A "switching" mechanism, if you will. Or a backup, if you really wanted to leave what you had in PRIMARY alone and untouched. Specification however wasn't followed and various apps did it in various ways.
  2. Windows / Macs popularized clipboard.
  3. This led to an interesting case: XA_PRIMARY was used for selections (as designed), rarely anybody had a good idea for XA_SECONDARY, and XA_CLIPBOARD was... like a clipboard.

xclip and xsel work on XA_PRIMARY by default, since most X-apps work this way: if you highlight a text, it is selected and therefore lands in a PRIMARY SELECTION buffer.

If you wish to paste PRIMARY, use middle mouse button or Shift+Insert. Or transfer it's contents to CLIPBOARD and just paste xclip -o | xclip -se c.

Excellent reading: https://specifications.freedesktop.org/clipboards-spec/clipboards-latest.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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