Is the linux clipboard a file?

We all know the saying: in linux, everything is a file.

My question is: how is the clipboard handled in linux? Is it an accessible and definite file? Would it be possible to read it with a cat or to write in it with a redirection, for example? (Given you have sufficient privilege)

If my assumption is correct, where is it located, and is it implementation distribution-specific?


Solution 1:

No. Unless you have a 3rd party program that copies the clipboard's contents to a file somewhere.

Here's the basic answer from askubuntu's Where are the contents of the clipboard stored?:

You seem to be lacking an understanding of clipboards in Linux. There are 3 usually available to users.

These clipboards are called "Selections"

There is:

  • PRIMARY - This is normally used for middle mouse button = copy/paste
  • SECONDARY - Normally not used by much, but it exists. Usually as an in app specific copy and paste.
  • CLIPBOARD - Usually Ctrl+c and Ctrl+v style copy and paste.

All of them are stored, by default in memory of the controlling application (usually Xorg, but not always). And every child application has access to it.

Clipboard monitors and managers exist that can monitor the clipboards and store their contents in a file, or more commonly, let you review your clipboard history.


Here's a little more interesting info from archlinux's wiki on the Clipboard

History

In X10, cut buffers were introduced. These were limited buffers that stored arbitrary text and were used by most applications. However, they were inefficient and implementation of them varied, so selections were introduced. Cut buffers are long deprecated, and although some applications (such as xterm) may have legacy support for them, it is both not likely and not recommended that they be used.

Selections

Freedesktop.org describes the two main selections as follows:[1]

  • PRIMARY

    Used for the currently selected text, even if it is not explicitly copied, and for middle-mouse-click pasting. In some cases, pasting is also possible with a keyboard shortcut.

  • CLIPBOARD

    Used for explicit copy/paste commands involving keyboard shortcuts or menu items. Hence, it behaves like the single-clipboard system on Windows. Unlike PRIMARY, it can also handle multiple data formats.

The majority of programs for Xorg, including Qt and GTK+ applications, follow this behavior. While ICCCM also defines a SECONDARY selection, it does not have a consensually agreed upon purpose. Despite the naming, all three selections are basically "clipboards". Rather than the old "cut buffers" system where arbitrary applications could modify data stored in the cut buffers, only one application may control or "own" a selection at one time. This prevents inconsistencies in the operation of the selections.

See the Keyboard shortcuts page which lists the default shortcuts in many programs.

It is also important to realize that according to the selection protocols, nothing is copied until it is pasted. For example, if you select some word in a terminal window, close the terminal and then want to paste it somewhere else, it will not work because the terminal is gone and the text has not been copied anywhere. If you want the word to be preserved after closing terminal window, consider installing a clipboard manager.

Note: Clipboard managers can significantly change the user experience, for example they might synchronize the PRIMARY and CLIPBOARD selections to emulate a single-clipboard system.