Introduction
In the day-to-day work with Linux, you often need to copy text or commands from the terminal to other applications, such as a text editor, a browser or a chat client. Instead of selecting with the mouse and using Ctrl + Shift + C, there are command line tools that allow you to send the content directly to the clipboard of the graphic environment. The most popular arexclipandxsel. This article explains what they are, how to install them, their basic options and some advanced examples so that you can integrate them into your workflow.
What are xclip and xsel?
Both utilities act as a bridge between the standard input (stdin) and the X11 clipboard. When you redirect the output of a command toxcliporxsel, the text is available on the clipboard and can be glued with the usual shortcut (Ctrl + V or Shift + Insert). The main difference lies in their options and in the predetermined behavior of the different buffers (primary, secondary and clipboard).
Installation
In most Debian-based distributions, the package is calledxcliporxsel. In Red Hat / Fedora systems the names are identical. Typical installation commands are:
- Debian / Ubuntu:
sudo apt update && sudo apt install xclip xsel - Fedora:
sudo dnf install xclip xsel - Arch Linux:
sudo pacman -S xclip xsel
After installation, check that the binaries are in the PATH runningwhich xclipandwhich xsel.
Basic use of xclip
The easiest way is to redirect the output of a command to xclip:
echo \"Texto de ejemplo\" | xclip
This call uses the buffer by defaultPrimary(selection). To paste, just click medium or use Shift + Insert in most environments. If you want to use the clipboard that responds to Ctrl + V, you must specify the option-selection clipboard:
echo \"Texto para Ctrl+V\" | xclip -selection clipboard
You can also read the contents of the clipboard with the option-o(output):
xclip -o -selection clipboard
Basic use of xsel
xsel works very similar, but its options are a little more concise. To copy to the standard clipboard:
echo \"Texto para xsel\" | xsel --clipboard --input
To read the clipboard:
xsel --clipboard --output
If you miss--clipboard, xsel acts on the primary buffer (selection).
Practical examples
-
- Copy the current directory route:
pwd | xclip -selection clipboard
- Save the output of a long command on the clipboard:
grep -r \"error\" /var/log --include=\"*.log\" | xsel --clipboard --input
- Exchange content between two terminals:
# En la primera terminal
cat archivo.txt | xclip -selection clipboard
# En la segunda terminal
xclip -o -selection clipboard > copia.txt
- Use xclip in an alias to simplify the command:
alias cpclip='xclip -selection clipboard'
# Luego
ls -l | cpclip
Key differences between xclip and xsel
Although they perform the same function, there are nuances that can influence the choice:
- Units:xclip requires the X11 library, while xsel can be compiled with Wayland support by additional extensions.
- Speed:In simple tests, xsel tends to be slightly faster when handling large volumes of data.
- Syntax:Some users find the most intuitive xsel syntax because the options
--inputand--outputThey're explicit. - Availability:In minimum server facilities without graphic environment, neither one works without an active X or Wayland server.
Tips and good practices
- Always check which buffer you are using; confusing the primary with the clipboard can lead to sticking unexpected text.
- In scripts that run in environments without GUI (e.g. cron), redirect the output to / dev / null or use tools such as
pbcopyin macOS orclipon Windows if you need to multiply platforms. - Combine xclip / xsel with
ssh -Xor send X to copy text from a remote session to the local clipboard. - If you work with pure Wayland, consider alternatives like
wl-clipboard(commandswl-copyandwl-paste) which are the native equivalents.
Conclusion
Domain xclip and xsel allows you to save time and reduce mouse dependence when working in the terminal. Whether you need to copy a route, a command result or a code fragment, these tools offer a fast and reliable way to interact with the clipboard in your graphic environment. Try the examples shown, adapt the aliases to your workflow and discover which of the two utilities best suits your needs.


