xsel
Access and manipulate X server selections
TLDR
Use a command's output as input of the clipboard (equivalent to
Use the contents of a file as input of the clipboard
Output the clipboard's contents into the terminal (equivalent to
Output the clipboard's contents into a file
Clear the clipboard
Output the X11 primary selection's contents into the terminal (equivalent to a mouse
SYNOPSIS
xsel [-p|-s|-b] [-i|-o] [-f|-c|-k|-l|-t milliseconds|-a|-u] [file...]
PARAMETERS
-p
Primary Selection. Operates on the PRIMARY selection, which is the default if no selection option is specified. Typically used for highlight-and-paste.
-s
Secondary Selection. Operates on the SECONDARY selection, which is less commonly used.
-b
Clipboard Selection. Operates on the CLIPBOARD selection. This is the common Ctrl+C/Ctrl+V mechanism.
-i
Input. Reads from standard input (or files specified as arguments) and sets the selected buffer's content.
-o
Output. Writes the content of the selected buffer to standard output.
-f
Fork. Forks to the background immediately after setting the selection. Useful for scripts where the parent process needs to continue without waiting for xsel.
-c
Clear. Clears the content of the specified selection buffer.
-k
Kill. Kills any existing xsel process that is currently owning a selection.
-l
List. Lists the owners of the PRIMARY, SECONDARY, and CLIPBOARD selections.
-t milliseconds
Timeout. Specifies a timeout in milliseconds to wait for a selection owner to appear when attempting to retrieve a selection.
-a
Append. Appends the input to the current content of the selection. Requires that xsel becomes the selection owner first.
-u
Update. Forces xsel to update its selection and act as a new selection owner, useful for certain applications.
--version
Prints the xsel version information to standard output.
--help
Prints a brief usage message and exits.
DESCRIPTION
xsel is a command-line utility for interacting with the X Window System's selection buffers. It allows users to store text into and retrieve text from these buffers, effectively enabling copy-paste operations between terminal applications and graphical user interface (GUI) applications.
The X Window System defines several selection buffers, primarily:
Primary Selection: This is typically used for highlight-and-paste behavior, where text is selected by simply highlighting it with the mouse.
Secondary Selection: Less commonly used, often for specialized application functions.
Clipboard Selection: This is the familiar copy-paste mechanism (Ctrl+C, Ctrl+V), where data persists even after the source application closes or the selection changes.
xsel defaults to operating on the Primary selection but can be directed to use the Clipboard or Secondary selections. It can read input from standard input or a file and write the selection contents to standard output. This makes it a powerful tool for scripting and automating interactions between the command line and the graphical desktop environment.
CAVEATS
xsel relies on the X Window System. It will not function in environments without a running X server (e.g., pure TTY consoles or SSH sessions without X forwarding). While robust for its intended purpose, it may exhibit different behaviors or limitations when interacting with applications that implement X selections in non-standard ways. For more advanced features like specifying target types (e.g., HTML, images), the xclip utility might be more suitable.
<B>BASIC USAGE EXAMPLES</B>
To copy text from a file to the clipboard:
cat myfile.txt | xsel -b -i
To paste the clipboard content into a new file:
xsel -b -o > pasted_text.txt
To copy the last command executed to the primary selection:
history 1 | awk '{print $2}' | xsel
To clear the clipboard:
xsel -b -c
<B>X SELECTION TYPES</B>
The X Window System defines several selection 'atoms' or types that xsel interacts with:
PRIMARY: The default selection, often used for immediate highlight-and-paste.
SECONDARY: A less commonly used auxiliary selection.
CLIPBOARD: The standard clipboard for explicit copy/cut and paste operations, persisting content until explicitly overwritten.
HISTORY
xsel is a long-standing utility within the X Window System's ecosystem, often distributed as part of the xorg-x11-utils or similar packages. It provides a simple yet effective command-line interface to the X selection mechanisms, a fundamental part of the X desktop experience. While the more feature-rich xclip utility emerged later, offering more fine-grained control over selection targets and formats, xsel has maintained its relevance due to its simplicity, minimal dependencies, and direct approach to handling basic text selections. Its development has been consistent with the evolution of Xorg, ensuring compatibility across various Linux and Unix-like systems.