When you spend a lot of time in a Linux terminal, it can become laborious to take your hand off the keyboard to grab the mouse. The habit of selecting text, right-clicking (or pressing keys) to copy, and then right-clicking (or pressing keys) to paste is a strong one, and it seems there’s always something that needs copying and pasting. Thanks to the wl-copy command, you can now copy things to your system clipboard, right from your Linux terminal, without using your mouse.
What is wl-copy?
The wl-copy command is part of the wl-clipboard package, which is a utility for the Wayland graphics server. Wayland is the layer on the Linux operating system that provides everything you see on your screen after you’ve logged in to a system. The wl-clipboard package contains both wl-copy for copying and wl-paste for pasting.
Install wl-clipboard
To make the wl-copy (and wl-paste) command available on your system, you must install the wl-clipboard software package. Before doing that, however, you must confirm that you’re running Wayland and not its predecessor, X11. You can do this by verifying that echo $XDG_SESSION_TYPE returns wayland:
$ echo $XDG_SESSION_TYPE
wayland
As long as you get wayland back from that command, then you can use wl-copy and wl-paste. On Fedora and other RPM-based Linux distributions (such as CentOS, OpenSUSE, Mageia, and OpenMandriva), you use dnf package manager:
sudo dnf install wl-clipboard
On Debian and Debian-based distributions, you use the apt command:
sudo apt install wl-clipboard
Using wl-copy
Once it’s been installed, you can use the wl-copy command to copy data to your clipboard. Anything you copy to your system clipboard is available everywhere on your system. You can copy data from one file from a terminal, without ever opening that file in a window of its own, and then paste that data into another application or into another file. That doesn’t mean that wl-copy is the only way to do that, but for now suppose that’s the workflow you need.
To copy an arbitrary string to your clipboard using wl-copy:
$ wl-copy "hello world"
The string “hello world” is now in your system clipboard. You can navigate to any other window on your desktop, right-click and select Paste (or press Ctrl+V), and you’ll get “hello world” pasted from your clipboard. Alternately, you can just press Ctrl+Shift+V in your terminal to paste the string there.
Copy command output with wl-copy
This can be useful in shell scripts, for example, where you’re generating output with the expectation that the output is meant to be inserted somewhere else on the system. Suppose your workplace uses two-factor authentication, and you prefer to use a command (rather than a hardware key, or a smart phone app), like pass otp to generate tokens. When the pass otp command outputs a token, you’d normally have to select it and copy it with a mouse before you could paste it into the login screen of your workplace intranet. With wl-copy, however, you can send the output directly to your system clipboard instead:
$ pass otp example.com | wl-copy
Copy data from a file with wl-copy
You can also copy the contents of a file using the wl-copy command so you don’t have to launch a separate application just to access a file’s contents. Of course, you’re still technically “opening” the file that you’re copying, but wl-copy does that part, invisibly, for you. Using your terminal, you can redirect the contents of a file into wl-copy:
$ wl-copy < example.txt
Here’s an example, creating a simple file using echo:
$ echo "This is example text." > example.txt
$ wl-copy < example.txt
$ wl-paste
This is example text.
It doesn’t have to be just text. You can copy any kind of data. For example, you can copy a JPEG image:
$ wl-copy < example.jpg
You could then paste the image into an application, like GIMP or LibreOffice, that can accept images.
Copying text in a terminal on X11
The wl-copy command is essentially the modern equivalent of the xclip command for the X11 graphic server. If you’re still running X11, you can use xclip instead. The syntax is a little different, but the concept is the same.
To copy an arbitrary string to your clipboard using xclip, you must pipe data to it and use the -i option to read from standard input:
$ echo "hello world" | xclip -i
To copy the contents of a file, you can redirect:
$ xclip -i < example.txt
To paste from your clipboard, use the -o option:
$ xclip -o
hello world
Copying from the terminal
Sometimes wl-copy is surplus to requirements. If you just need to copy text quickly from a terminal and then back into it, then Bash has built-in functions (like Ctrl+K and Ctrl-W and Alt+D) to copy text and then to “yank” it back again (Ctrl+Y). But when you want to get data from within a terminal and then paste it somewhere else, wl-copy is a real convenience. The wl-copy (or xclip) command can save you several clicks and seconds of launch times.