Xsel - Command-line Clipboard and Selection Tool

Learn how to use xsel, a command-line utility for accessing X clipboard and selection buffers. Convert data between stdin, stdout, and X selections easily.

Xsel - Command-line Clipboard and Selection Tool

Xsel is a powerful command-line utility for interacting with the X Window System's clipboard and selection buffers. It allows you to easily transfer data between your terminal and the graphical environment, mimicking the actions of copy (Ctrl+C) and paste (Ctrl+V), as well as the primary selection buffer often used with mouse selections.

Accessing X Clipboard with Xsel

The primary function of xsel is to manage the X clipboard. You can pipe data into xsel to copy it to the clipboard, or use xsel to output the clipboard's contents to standard output.

Copying Data to the Clipboard

To copy data to the clipboard, you can pipe it to xsel using the -i (input) and -b (clipboard) flags. This is equivalent to pressing Ctrl+C.

# Example: Copying the number 123 to the clipboard
echo 123 | xsel -ib

# Example: Copying the contents of a file to the clipboard
cat your_file.txt | xsel -ib

Pasting Data from the Clipboard

To retrieve data from the clipboard and display it on your terminal (equivalent to pressing Ctrl+V), use the -o (output) and -b (clipboard) flags.

# Example: Displaying clipboard contents on the terminal
xsel -ob

# Example: Saving clipboard contents to a file
xsel -ob > clipboard_content.txt

Managing X Selections with Xsel

Beyond the standard clipboard, Xsel also handles the X11 primary selection. This selection is typically populated when you highlight text with your mouse and can be pasted using the middle mouse button.

Using the Primary Selection

To interact with the primary selection, use the -p flag instead of -b.

# Example: Sending the primary selection to standard output
xsel -op

Clearing the Clipboard

You can clear the contents of the clipboard using the -c (clear) flag.

# Example: Clearing the clipboard
xsel -cb

Further Resources