This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
xclip connects the shell with the clipboard of the graphical desktop. Pipe the output of a command into it and paste the result in a browser, an editor or a chat window, without selecting text in the terminal with the mouse.
Install
sudo apt install xclip # Debian, Ubuntu
sudo dnf install xclip # Fedora
sudo pacman -S xclip # Arch
xclip talks to the X server, so it needs a graphical session running on X11. On Wayland, the default on current GNOME and KDE desktops, use wl-copy and wl-paste instead; they are covered at the end of this post. To know which one you have:
echo $XDG_SESSION_TYPE
Two clipboards
This is the detail that confuses everybody the first time. X11 has more than one "clipboard":
- PRIMARY holds whatever you last selected with the mouse. You paste it with the middle mouse button.
- CLIPBOARD is the one filled with
Ctrl+Cand pasted withCtrl+V.
xclip uses PRIMARY unless you say otherwise. So this:
uptime | xclip
can be pasted with the middle button, but Ctrl+V still pastes whatever was there before. To use the normal clipboard add -selection clipboard, which can be shortened to -sel clip:
uptime | xclip -selection clipboard
The rest of the examples use the short form.
Copy
The output of a command:
free -h | xclip -sel clip
A whole man page, to paste in a document:
man htop | xclip -sel clip
The contents of a file, either way works:
xclip -sel clip < notes.txt
xclip -sel clip notes.txt
Your SSH public key, ready to paste into a web form:
xclip -sel clip < ~/.ssh/id_ed25519.pub
Paste
-o writes the clipboard contents to standard output. From there it can go to a file or to another command:
xclip -sel clip -o
xclip -sel clip -o > file.txt
xclip -sel clip -o | wc -w
Select some text on a web page, press Ctrl+C, and the second command saves it to a file. With text you only highlighted, skip the option and read PRIMARY:
xclip -o > file.txt
The trailing newline
Most commands end their output with a newline, and xclip copies it too. Paste that into a terminal and the line runs immediately. The -r option removes the last newline:
pwd | xclip -r -sel clip
On old versions without -r, strip it yourself:
pwd | tr -d '\n' | xclip -sel clip
Copy and still see the output
-f prints the input back to standard output, so the command behaves like tee with the clipboard:
ip -br addr | xclip -f -sel clip
Images and other types
The clipboard can hold more than text. Tell xclip the type with -t:
xclip -sel clip -t image/png screenshot.png
Now Ctrl+V in an image editor or chat application pastes the picture. The other direction saves an image that was copied in another application:
xclip -sel clip -t image/png -o > pasted.png
List the types the current clipboard contents are offered in:
xclip -sel clip -o -t TARGETS
Handy aliases
If you come from macOS you will miss pbcopy and pbpaste. Add them to ~/.bashrc:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
cat report.txt | pbcopy
pbpaste | sort | uniq
Things to know
It needs a display. On a text console or over a plain SSH session you get Error: Can't open display. Over SSH, xclip works with X forwarding (ssh -X), and then it uses the clipboard of your local machine.
It stays running. In X11 the clipboard is not a storage area: the program that copied the data has to hand it over when someone pastes. xclip therefore remains in the background until another program copies something else. That is normal. If no clipboard manager is running, the contents are gone when that background process ends.
Passwords. Anything on the clipboard can be read by every program in your session, and clipboard managers keep a history. Think twice before piping secrets into it.
On Wayland: wl-copy and wl-paste
Install the wl-clipboard package:
sudo apt install wl-clipboard
sudo dnf install wl-clipboard
sudo pacman -S wl-clipboard
The two commands use the normal clipboard by default, so they are shorter to type:
free -h | wl-copy
wl-copy < notes.txt
wl-paste > file.txt
-n drops the trailing newline, --primary uses the primary selection, and -t sets the type:
pwd | wl-copy -n
wl-copy -t image/png < screenshot.png
wl-paste --list-types
See also
man xclip, man wl-copy. xsel is another tool for X11 that does the same job with different options.