Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

Buffers exist at every level of computing. They store data temporarily for future operations. Clipboards are such buffers. In this article, we’re going to explain ways to use a clipboard from a terminal under Linux. Towards this end, we’ll discuss clipboard implementations and terminals.

The code in the article has been tested on Debian 10.10 (Buster) with GNU Bash 5.0.3. It’s POSIX-compliant and should work in any such environment.

2. Clipboards

What distinguishes clipboards from other buffers is their ease of use and ubiquity.

Clipboards are part of every major operating system. They allow data transfer in and between applications. Default (often universal) keyboard shortcuts and mouse gestures ease the process.

2.1. System-Wide Clipboard

The system-wide clipboard (or system clipboard) is accessible across all applications. In particular, the X Window System, via its X Server component, provides the system clipboard in Linux. It has three parts (or selections): PRIMARY, SECONDARY, and CLIPBOARD. CLIPBOARD usually functions like what most of us would call “the clipboard”.

Importantly, the system clipboard isn’t part of the Linux kernelThe X Server provides the standard system clipboard available to applications under Linux.

2.2. Independent Clipboards

Independent clipboards serve a particular application and don’t cross application boundaries. Moreover, an independent clipboard can leverage, override, or ignore the system clipboard contents. Any application can implement its own separate clipboard mechanism.

As a result, independent clipboards are especially valuable in pure text-based terminals. We’ll discuss them below.

3. Terminals

Terminals are a command-line interface (CLI) — they only accept text input.

A terminal was originally only a screen and keyboard. It had just enough memory to remotely communicate with a computer. The closest thing to classical terminals in Linux is the Linux console. It’s implemented entirely in the kernel. Because of this, the Linux console doesn’t support a system-wide clipboard on its own.

Classic terminals have been superseded by terminal emulators. They can run on any local or remote machine. Terminal emulators can add functionality not present in the terminal they are emulating.

4. System Clipboard CLI Access

Standard CLI system clipboard access is done via xsel (X selections) and xclip (X clipboard CLI). Both depend on the X Server running either remotely or locally. Their basic usage is similar and straightforward:

[email protected]:~$ echo Copied via xsel. | xsel --input
[email protected]:~$ xsel --output
Copied via xsel.
[email protected]:~$ echo Copied via xclip. | xclip -in
[email protected]:~$ xclip -out
Copied via xclip.

Trying to use xsel or xclip without X Server running (checked via xset), produces an error:

[email protected]:~$ xset q
xset: unable to open display ""
[email protected]:~$ echo Copied via xsel. | xsel --input
xsel: Can't open display: (null)
: Inappropriate ioctl for device

Tools like xsel and xclip are the only choice for using the X Server system clipboard in a pure text-based terminal.

On the other hand, terminal emulators like GNOME Terminal Emulator and Konsole provide additional options. Their graphical user interface (GUI) nature includes native mouse and, often, keyboard support. Because of this, users can easily select, store, and restore text via the system clipboard. The functionality also extends to remote terminal emulators (such as Putty).

5. Independent Clipboard CLI Access

Even when a system clipboard is unavailable, there are options for clipboard-like functionality.

5.1. General-Purpose Mouse (gpm)

It turns out that there’s a loophole in the “text input only” rule for terminals. It allows for perhaps the most natural way to store and restore data via the clipboard — the mouse.

The gpm software package provides a rudimentary mouse driver to any supported terminal. When running the gpm daemon, an inverted highlighter character represents the mouse cursor. It roughly reflects the movements of the mouse along with the character matrix.

[email protected]:~$ echo This is the output of a command.
This is the output of a command.               █
[email protected]:~$

The gpm package has clipboard functionality limited to the currently visible data. The user can select text to directly transfer to the prompt position. The data isn’t stored anywhere after this operation.

5.2. Screen Manager (screen) and Terminal Multiplexer (tmux)

Terminal input can contain complex code symbols (think terminal codes and signal shortcuts). They can, for example, change the cursor position at a specific place on the screen.

Terminal multiplexers like screen and tmux use these instructions with key bindings. They enable the user to have several virtual consoles from different processes. The virtual consoles are placed in window-like portions of the same terminal screen.

Each virtual console in screen and tmux provides a copy-and-paste mechanism. The user can scroll through the history of a window, select the text, and store it in a paste buffer. The paste buffer’s contents are held in the main terminal multiplexer process. Subsequently, they can be restored in any window.

5.3. Vi IMproved (vi)

The vi text editor also allows for copy-and-paste operations within the application. For example, from vi in normal mode (accessed via Esc), the user can:

  • yy – copy (yank) the current line, including the newline character
  • p – paste (put) the line(s) in the buffer into the text after the current line

5.4. Custom Implementations

Any storage type can serve as a type of primitive clipboard.

In Bash, for example, we can implement a clipfile:

[email protected]:~$ echo Copied via clipfile. > /tmp/clipfile
[email protected]:~$ cat /tmp/clipfile
Copied via clipfile.

The drawbacks, however, include inconvenience, lack of standardization, and limited usefulness.

6. Conclusion

In this article, we explained different ways to use clipboards in a terminal. First, we defined what a clipboard is and described two different types of clipboards. We then used tools for system clipboard access via the terminal. Finally, we discussed alternative clipboard implementations.

In summary, terminal clipboard access is possible in multiple ways. Depending on the circumstances, it can even be convenient.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!