In this guide, we are going to learn what Xclip and Xsel programs are, and how to manipulate and access Clipboard contents using Xclip and Xsel programs in Linux.
What are Xclip and Xsel programs?
Xclip is a command line interface to X selections i.e. Clipboard. Xclip reads the data from one or more files and makes the data available as an X selection for pasting the data into X applications. If no files are specified, it reads data from the standard input. It can also print the X selection to the standard output.
In Linux, the clipboards are known as "Selections" and there are three types of clipboards available in X11 window system. They are PRIMARY, SECONDARY, and CLIPBOARD. For more details about Clipboard, check this link.
Xsel is a command line X11 selection and clipboard manipulation tool. It is used to access X clipboard and selection buffers in Linux and Unix-like operating systems.
With the help of Xclip and Xsel programs, we can easily imitate the functionality of pbcopy and pbpaste commands in Linux. For those wondering, pbcopy and pbpaste are used to manipulate clipboards and they are exclusively available only for Mac OS X platform.
Install Xclip and Xsel in Linux
Xclip and Xsel programs are available in the official repositories of most modern Linux distributions.
On Alpine Linux, run the following command to install Xclip and Xsel:
$ sudo apk add xclip xsel
To install Xclip and Xsel in Arch Linux and its variants like Manjaro Linux, run:
$ sudo pacman xclip xsel
On RHEL, CentOS, AlmaLinux and Rocky Linux:
$ sudo dnf install epel-release
$ sudo dnf install xclip xsel
$ sudo dnf xclip xsel
On Debian, Ubuntu, Pop!_OS, Linux Mint:
$ sudo apt install xclip xsel
$ sudo zypper install xclip xsel
Manipulate and Access Clipboard Contents using Xclip and Xsel
Even though Xclip and Xsel are similar programs and does the same job, their usage is slightly different from each other. First, let us discuss the usage of Xclip program.
1. Xclip command examples
Xclip handles the PRIMARY, SECONDARY Selections, plus the system Clipboard.
1.1. Copy data to clipboard using Xclip
To copy the output of a command to clipboard using Xclip, run:
$ echo "Welcome To OSTechNix" | xclip -selection clipboard
You can also use this short version of the above command:
$ echo "Welcome To OSTechNix" | xclip -sel c
-sel represents the
As per the above command, Xclip copied the output from the
echo command i.e. "Welcome To OSTechNix", to the X11 primary selection area (i.e. clipboard). Similarly, you can copy/send output of any other command to the clipboard. Here is another example:
$ uname -r | xclip -sel c
In our above examples, we copied copy the output from stdin into clipboard buffer. What about the contents of a file? It is also possible.
To copy file contents into clipboard using
Xsel command, run:
$ xclip -selection clipboard < ostechnix.txt
Or shortly use this:
$ xclip -sel c < ostechnix.txt
The above commands will not display contents of the files. Instead, they will only copy the file contents to system clipboard. You can read more details about copying file contents into clipboard in this link.
1.2. Paste data from clipboard to console using Xclip
We know now how to copy the data from standard output and a file to clipboard. How to retrieve the copied data from clipboard? That's simple! Run the following command to paste the contents of the system clipboard to the console:
$ xclip -o -sel clip
$ xclip -o -sel c
If you want to paste the contents of the X11 primary selection area to the console, run:
$ xclip -o
1.3. Paste data from clipboard to file using Xclip
Instead of displaying (pasting) the contents of clipboard, you can also directly paste the contents of the system clipboard or X11 primary selection area into a file like below:
$ xclip -o -sel clip > output_file.txt
$ xclip -o > output_file.txt
">" mark will overwrite the existing contents of the output file. Instead of overwriting, you can simply append data into the output file using double
$ xclip -o >> output_file.txt
For more details, refer Xclip manual page:
$ man xclip
2. Xsel command examples
By default, Xsel operates on X PRIMARY selection unless you exclusively specify the X selection.
2.1. Copy data to clipboard using Xsel
To copy the output of a command into clipboard using Xsel, run:
$ echo "Welcome To OSTechNix" | xsel -ib
The above commands reads from STDIN and save it to the clipboard; as if
Ctrl + C.
To copy the file contents to clipboard, run:
$ cat input_file.txt | xsel -ib
i represents input and
b represents clipboard.
You can also use the following commands:
$ xsel --clipboard < input_file.txt
Or, shortly use this:
$ xsel -b < input_file.txt
-b represents clipboard.
2.2. Paste data from clipboard to console using Xsel
To paste or display the contents of the clipboard in the Terminal (equivalent to
Ctrl + V), run:
$ xsel -ob
o represents output and
b represents clipboard.
2.3. Paste data from clipboard to file using Xsel
To paste the clipboard's contents into a file:
$ xsel -ob > output_file.txt
If you don't want to overwrite the existing contents of the output file, simply append the data using double
">>" symbols like below:
$ xsel -ob >> output_file.txt
If you want to paste the contents of X11 primary selection into the terminal (equivalent to a mouse middle-click), use
-p (PRIMARY selection) instead
$ xsel -op
2.4. Clear clipboard using Xsel
To clear the contents of the clipboard, run:
$ xsel -cb
For more details, refer Xsel manual page:
$ man xsel
In this guide, we discussed how to manipulate and access Clipboard contents using Xclip and Xsel programs with examples in Linux and Unix-like operating systems.
These tools do not work with Wayland.
For the Wayland protocol stack you’ll want wl-clipboard (https://github.com/bugaevc/wl-clipboard) which has a very different syntax and is not a drop-in replacement.