10
  • Primary
  • Secondary
  • Clipboard
  • Cut buffers

What is the difference between these?

Experimenting with xclip and gedit:

Setting primary

Nothing happens, as far as I can tell

Setting secondary

Again, as far as I can tell, does not affect gedit

Setting clipboard

paste in edit and context menu is disabled, but pressing control-v causes the text I want to paste.

Copying in gedit

Appears to be setting primary and clipboard

What is each used for? How are they usually accessed in programs? How is it implemented? Are they properties on the root window (if so, what are their names)?

Cut buffers

I understand clip buffers are used in xterm when you select text, and are pasted when you middle click. Do any other programs use cut buffers?

2 Answers 2

14

In X11 terminology, there are many selections, but only one of them is called the clipboard. (Primary and secondary are the other two selections. Cut buffers are not selections; they're a different and obsolete mechanism)

Clipboard & Primary

The clipboard selection is used for explicit "Cut", "Copy", and "Paste" operations, exactly like in other systems. You copy data into the 'CLIPBOARD' selection using CtrlC or "Edit>Copy" and paste from it with Ctrl+V or "Edit>Paste". In other words, it's literally your standard clipboard.

(The right-click menu should have operations identical to the Edit menu, both accessing the same clipboard. If they behave differently, I would call that a Gedit bug.)

In contrast, the primary selection will automatically hold whatever text you have selected last. (Normally the primary selection is only used for text, not images or other data types.)

You paste from primary selection by middle-clicking the mouse. (For mice with a scroll wheel, the wheel is also the middle button. For mice or touchpads with only two buttons, pressing both at once emulates the middle button.) There is neither a menu item nor a standard keyboard shortcut for pasting from primary.

(Because the primary selection is meant to directly reflect currently highlighted text, most X11 apps will actually remove the highlight when something else is put into primary. So while in Windows you can highlight some text in window A without losing the previous highlight in window B, you can't do that in X11.)

In terminal apps, ShiftIns usually pastes from primary selection, while CtrlShiftV pastes from the clipboard. (In other programs, the behavior of 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 anymore, but most likely it comes from the SunView graphical environment that predated X and was exclusive to Sun workstations.

SunView had selections very similar to the ones in X11 – it had Primary, Secondary, and Shelf (which was the SunView name for the clipboard). However, instead of using combination hotkeys like Ctrl+V, Sun keyboards had dedicated physical keys for Cut, Copy and Paste on the left side of the keyboard.

In SunView, the secondary selection worked a bit like primary selection, 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 immediately paste the selected text at the cursor location. By doing this, you would not lose the existing primary selection.

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

Cut buffers are a different mechanism than any of the above – they're also a relic from X10 (before the selection mechanism was added in X11) and almost nothing uses them anymore.

Some very old programs (mainly those which date back to X10) still use cut-buffers, e.g. selecting text in Xedit will update both CUTBUFFER0 and the "modern" primary selection, while pressing Shift+Ins will paste from primary if it exists and from CUTBUFFER0 otherwise.

Whereas selections are a type of inter-process communication (the destination program directly requests "pasted" data from the source program) and can negotiate different data types, cut buffers are static (stored as "root window properties" by the X server) and are only suitable for storing short strings.


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.

3

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.

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.