xclip
Copy data to/from the X clipboard
TLDR
Copy the output from a command to the X11 primary selection area (clipboard)
Copy the output from a command to a given X11 selection area
Copy the output from a command to the system clipboard, using short notation
Copy the contents of a file into the system clipboard
Copy the contents of a PNG into the system clipboard (can be pasted in other programs correctly)
Copy the user input in the console into the system clipboard
Paste the contents of the X11 primary selection area to the console
Paste the contents of the system clipboard to the console
SYNOPSIS
xclip [OPTION]... [FILE]
PARAMETERS
-i
, --in
Reads data from standard input and places it into the specified X selection. This is the default action if no -o
is given.-o
, --out
Prints the content of the specified X selection to standard output.-selection {primary|secondary|clipboard}
Specifies which X selection buffer to use. Default is primary.-d
, --display {display name}
Specifies the X display to connect to, e.g., :0.0
. Defaults to the value of the DISPLAY
environment variable.-f
, --filter
Operates in filter mode. Reads from standard input, copies to selection, then prints the selection content to standard output. Useful in pipelines.-target {atom name}
Specifies the target atom to use when retrieving the selection. Useful for specific data types like TARGETS
, UTF8_STRING
, or text/uri-list
.-l
, --loops {number}
Specifies how many times to loop when waiting for a selection request. A value of 0 means infinite loops, effectively holding the selection indefinitely.-n
, --no-newline
When used with -o
, prevents xclip
from appending a newline character to the output.
DESCRIPTION
xclip
is a versatile command-line utility designed to interact with the X Window System's selection buffers. It provides a convenient way to integrate command-line operations with graphical applications by enabling text to be copied to or pasted from the clipboard. This means you can pipe output from any command into xclip
to make it available for pasting in a GUI application, or conversely, retrieve text from an X selection into your shell for further processing.
It primarily handles three types of X selections: primary, secondary, and clipboard. The primary selection is typically used for highlighted text that can be pasted with a middle-click. The clipboard selection is the standard copy/paste buffer often associated with Ctrl+C/Ctrl+V operations. xclip
allows seamless transfer of data, making it an indispensable tool for automating tasks and enhancing productivity in X-based environments.
CAVEATS
xclip
requires an active X server to function. The content placed on the X selection is generally owned by the application that put it there. If xclip
is used to place data on the selection, it must continue running to 'own' that selection until another application requests it or xclip
exits. For persistent, background clipboard management, a dedicated clipboard manager application is often preferred over xclip
alone.
While xclip
generally handles UTF-8 encoding well, issues might arise with certain locales or applications that do not correctly announce their text encoding.
UNDERSTANDING X SELECTIONS
The X Window System manages multiple "selection" buffers for copy-paste operations:
Primary Selection: The default selection. Text highlighted with the mouse is automatically placed here and can be pasted typically with a middle-click.
Clipboard Selection: The standard copy/paste buffer, analogous to Ctrl+C/Ctrl+V. Content must be explicitly copied into it.
Secondary Selection: Less commonly used, provides an additional, auxiliary selection buffer.xclip
allows you to explicitly choose which of these selections you want to interact with using the -selection
option.
COMMON USE CASES
Copy command output to clipboard:
ls -l | xclip -selection clipboard
Paste clipboard content to terminal:
xclip -selection clipboard -o
Copy file content to primary selection:
xclip < ~/.ssh/id_rsa.pub
Copy URL from browser (primary) to clipboard:
xclip -o -selection primary | xclip -i -selection clipboard
HISTORY
xclip
was developed by Jonathan Goldie as a robust command-line interface for interacting with the X Window System's selection mechanism. It emerged as a practical tool for bridging the gap between console applications and graphical environments, allowing for seamless data transfer. Its design provides an alternative to and often more flexible approach than other similar utilities like xsel
, with features like filtering and explicit target control, making it a staple in many X user's command-line toolkits.
SEE ALSO
xsel(1), xprop(1), xclipboard(1), xbindkeys(1)