FROM AGPEDIA — AGENCY THROUGH KNOWLEDGE

xclip

xclip is a command-line utility for interfacing with the X Window System's clipboard mechanism from a Unix shell. [1:1] It reads data from standard input or from files and places it in an X selection, making it available for pasting into graphical applications; with the -o flag it does the reverse, printing the current selection to standard output. [2:1] xclip was written by Kim Saunders and first released in 2001. [3:1] It is free software released under the GNU General Public License version 2. [4:1]

Background: X11 selections

The X Window System does not implement a single clipboard. Instead, it defines a set of named buffers called selections, specified in the Inter-Client Communication Conventions Manual (ICCCM). Three standard selections are defined. [5:1]

Selection Typical behavior How to paste
PRIMARY Set automatically when text is highlighted Middle-click
SECONDARY Reserved; rarely used in practice
CLIPBOARD Set by explicit cut or copy commands (e.g., Ctrl+C) Ctrl+V or Edit → Paste

Selections use an asynchronous protocol: data is not copied to a central store but instead held by the owning application, which must respond to paste requests on demand. [5:2] A consequence is that clipboard contents disappear when the owning application exits, unless a clipboard manager (such as xclipboard, parcellite, or klipper) is running to intercept and re-serve the data.

xclip interacts with this system by running as a background process that claims ownership of the chosen selection and serves the data when another application requests a paste. [2:1]

Command-line interface

xclip's default mode is input (write to a selection). The most common invocation pipes output from another command:

command | xclip -selection clipboard

The -o flag switches to output mode, printing the selection contents to standard output:

xclip -o -selection clipboard

Key options are summarized below. [2]

Option Short form Description
-in -i Read from standard input or files (default)
-out -o Print the selection to standard output
-selection -sel Choose which selection: primary, secondary, or clipboard. Default is primary. Only the first letter is required, so -sel c selects clipboard.
-display -d Specify the X display to connect to (defaults to $DISPLAY)
-loops -l Exit after receiving this many paste requests (0 = unlimited, the default)
-target -t Specify a MIME type or X target atom for the data (e.g., image/png)
-rmlastnl Remove the trailing newline from the selection [2:2]
-filter -f In input mode, also print the piped data to standard output (pass-through)
-silent Suppress all output except errors; run in background (default)
-quiet Run in foreground and report activity
-verbose Print a running commentary of selection requests received

Options can be abbreviated to any unambiguous prefix. For example, -disp works as short for -display, but -v would be ambiguous between -verbose and -version and is therefore interpreted as a filename. [2]

Non-text content

xclip can handle binary or structured data by specifying a MIME type with -target. For example, to copy a PNG image so that it can be pasted into image-aware applications:

xclip -selection clipboard -target image/png photo.png

To inspect what data formats the current clipboard owner supports:

xclip -o -selection clipboard -target TARGETS

File transfer utilities

xclip ships with three companion shell scripts that use the X selection mechanism to transfer files and directory trees between hosts sharing an X11 connection. [1:2]

Utility Function
xclip-copyfile Pack one or more files or directories into the selection
xclip-pastefile Unpack files from the selection into the current directory
xclip-cutfile Like xclip-copyfile, but deletes the originals after packing

This technique works across machines when both ends are connected to the same X server (for example, via SSH with X forwarding enabled).

History

Kim Saunders created xclip in 2001 and packaged it for Debian shortly after its initial release. [3:1] The project was hosted on SourceForge before migrating to GitHub. After Saunders became inactive as maintainer, Baruch Even undertook upstream-style fixes in a separate repository. [4:1] Peter Åstrand subsequently took over active maintenance from 2007 through at least 2018, and the canonical repository is now hosted under his GitHub account at github.com/astrand/xclip. [1]

As of version 0.12 (copyright 2001–2008), the version string reads: "Copyright (C) 2001-2008 Kim Saunders et al. Distributed under the terms of the GNU GPL." [2]

Limitations

Wayland incompatibility

xclip is specific to X11 and does not operate under the Wayland display protocol. [6:1] On Wayland desktops that include an XWayland compatibility layer, xclip may still function through XWayland's X11 emulation, but this is not guaranteed and behavior can vary across compositors. The native Wayland equivalent is wl-clipboard, which provides wl-copy and wl-paste commands with an interface similar to xclip.

Clipboard persistence

Because X11 selections are held by the owning process rather than stored in the display server, the contents of the selection are lost the moment xclip exits. xclip handles this by staying alive in the background indefinitely (or until the configured number of paste requests is served). If a script pipes data into xclip and then the script exits, the selection data becomes unavailable unless a clipboard manager has already captured it. The -loops option provides control: -loops 1 causes xclip to exit after the first paste, which is useful in scripts where clipboard persistence beyond a single use is not needed.

Tool Platform Notes
xsel X11 Similar functionality to xclip with a somewhat different CLI; supports appending to and deleting from selections
wl-clipboard Wayland Provides wl-copy and wl-paste; the idiomatic replacement on Wayland desktops
pbcopy / pbpaste macOS Built-in macOS equivalents for clipboard access from the shell
xdotool X11 Broader X automation tool with clipboard manipulation among its features
xclipboard X11 Graphical clipboard manager bundled with X11; can act as a persistent clipboard owner
  1. ^ ↗ readme-description ^ ↗ readme-file-tools ^ Åstrand, Peter. astrand/xclip: Command line interface to the X11 clipboard. https://github.com/astrand/xclip.
  2. ^a ^b ↗ manpage-design-intent ^ ↗ manpage-rmlastnl ^a ^b ^c xclip(1) — Debian testing manpages. Debian Project. https://manpages.debian.org/testing/xclip/xclip.1.en.html.
  3. ^a ^b ↗ first-debian-package-date Copyright: xclip package in Lenny (Debian 5.0). Launchpad / Debian Project. https://launchpad.net/debian/lenny/+source/xclip/+copyright.
  4. ^a ^b ↗ copyright-authorship Copyright: xclip package in Sid (Debian). Launchpad / Debian Project. https://answers.launchpad.net/debian/sid/+source/xclip/+copyright.
  5. ^ ↗ three-selections ^ ↗ selection-async-protocol ClipboardsWiki. freedesktop.org. https://www.freedesktop.org/wiki/Specifications/ClipboardsWiki/.
  6. ^ ↗ xclip-wayland-limitation Clipboard — ArchWiki. Arch Linux. https://wiki.archlinux.org/title/Clipboard.
Available in