What is the difference between the X clipboards?

There are many selections, but only one clipboard.

Clipboard vs Primary

The clipboard is your regular clipboard – it's used for explicit "Cut", "Copy", and "Paste" operations. You copy to it with Ctrl+C or "Edit→Copy", paste with Ctrl+V or "Edit→Paste", and it can hold various kinds of data (text, image, files). Overall, the clipboard in X11 works just like it does in Windows and other systems.

(Technically the clipboard is implemented as yet another selection, so all CLI tools which work with primary/secondary selections should also be able to access the clipboard as well.)

In contrast, the primary selection is automatic, and as the name implies it holds whatever text you have selected last. (Only text normally goes into the primary selection – not graphics or other objects.)

You might have noticed that in X11, highlighting some text in one app automatically cancels a previous highlight even in other apps, which is something that doesn't happen in Windows. This is because the highlight in X11 directly corresponds to the "primary selection" (hence the name).

You usually paste from primary selection by middle-clicking the mouse. (For mice with a scrollwheel, the wheel is also the middle button. For mice or touchpads with only two buttons, pressing both at once emulates the middle button.)

In terminals, Shift+Ins usually pastes from primary selection, while Ctrl+Shift+V pastes from the clipboard. (In other programs, Shift+Ins varies – it could paste from clipboard, from primary, or even from a cutbuffer.)

Secondary

The secondary selection is a relic that nothing in X11 uses.

(Except Emacs – where it works like primary, but if you're holding the Alt or Meta key.)

It seems that the secondary selection comes from the SunView graphical environment that predated X and was exclusive to Sun workstations. SunView had selections just like X11 now does – it had Primary, Secondary, and Shelf (which was the SunView name for the clipboard).

Instead of combination hotkeys (like Apple's Ctrl+C or IBM's Ctrl+Ins), the Sun keyboards had several dedicated function keys on the left: Cut, Copy, Paste, Find were real physical keys.

In SunView, the secondary selection worked a bit like the primary one, but it was invoked by holding one of the function keys. For example, if you placed the cursor somewhere, then held Paste and went off to select some text, that would become the secondary selection – releasing Paste would indeed paste that text at the cursor location.

From SunView User's Guide:

6.7. Secondary Selections

A secondary selection is a temporary selection you make while holding down a function key. The action takes place when you release the function key. This feature lets you do many Copy, Paste, Cut, and Find shortcuts with function keys.

The secondary selection is always underlined. It lasts only as long as you hold down the function key. During that time, you can adjust the secondary selection the same way you adjust a primary selection.

You can also scroll while making a secondary selection, as long as you continue to hold the function key down. That way, you can adjust the selection to encompass characters that aren't currently visible in the window.

With secondary selections, you can do editing operations that require both a source and a destination, such as duplicating and moving text. Secondary selections also let you perfonn an operation without changing the current primary selection and insertion point.

(The manual continues with examples of primary and secondary selection usage in SunView, some of which is quite similar to X11.)

From SunView System Programmer's Guide, section 9.2:

The selection library deals with four objects under the general term "selection." Most familiar is the Primary selection, which is normally indicated on-screen by inverting ("highlighting") its contents. Selections made while a function key is held down (usually indicated with an underscore) are Secondary selections. The selection library treats the Shelf21 (the global buffer which is loaded by Cut22 and Copy operations, and which may be retrieved by a Paste operation) as a third kind of selection. Finally, the insertion point, or Caret, is also treated as a selection, even though it has no contents. These are the four ranks the selection library deals with: Caret, Primary, Secondary, and Shelf.

(Based on this, the "Shelf" in SunView would be roughly the equivalent of the "Clipboard" in X11.)

Cut buffers

Those go back to X10, and are a different thing from selections. I don't have much information about them so far, except that you shouldn't care about them.

Only some ancient X programs (e.g. those written in libXaw) use cut-buffers, with Shift+Ins pasting from one.


See also:

  • X Selections, Cut Buffers, and Kill Rings**

    Clipboard: for when the user has explicitly copied something (e.g., the ``Edit/Copy'' menu item.)
    Primary: more ephemeral and implicit: it is for when the user has highlighted or selected something (e.g., dragging the mouse over some text.)
    Cut Buffers: Obsolete. Never, ever, ever use them. Ever.

  • Wikipedia's article on X Window selection says that only cut buffers were window properties of the root window, named CUT_BUFFER1 and so on.


X calls them "selections" rather than "clipboards". From Wikipedia (the whole article will probably help you, too):

At the level of the core protocol, the PRIMARY and CLIPBOARD selections do not differ. But the xclipboard client makes them behave differently. In particular, when another client asserts the ownership of the CLIPBOARD selection, this program requests and displays it in a window. Any further request for this selection are handled by xclipboard. This way, the content of the selection survives the client having copied it.

There is no inherent difference between them, the only difference is in the behavior of applications using them. For example, you could write a program that, when you pressed ctrl v, used the primary selection instead of clipboard, and it would be perfectly fine from a technical standpoint. The difference is social, in that users expect certain behavior, and it's smart to follow that the vast majority of the time.

I thought selecting text and middle-clicking to paste it was using the primary selection, rather than cut buffers (which are deprecated), but I could be wrong, as I've never coded directly against the X protocol.