Skip to main content

How to use the clipboard in a terminal

·2 mins

Sometimes it’s handy to copy a command output or paste a piece of text into a unix pipe. Fortunately, there are handy commands to do it.

Xorg #

The interaction with the Xorg clipboard is done using the xclip command. The xclip command is in the xclip package in Fedora:

sudo dnf install xclip

To copy a command output to the clipboard, just use:

echo "I am now in your clipboard" | xclip

To get the clipboard content as a command output, the following command is useful:

xclip -o

Of course, this is much more useful with pipes. The following example pipes the clipboard content into the less pager:

xclip -o | less

Wayland #

If you use a more brave Linux distribution, you may use the Wayland project instead of Xorg. In this case, you can use the wl-clipboard project. You can install it in Fedora from the wl-clipboard package:

sudo dnf install wl-clipboard

To copy a command output to the clipboard, you can use wl-copy:

echo "I am now in your wayland clipboard" | wl-copy

Pasting is done using the wl-paste command:

wl-paste | less

Using these tools to get your public key to the clipboard #

I often spin up virtual machines in a cloud. To be able to log in to them using my public SSH key, it’s usually needed to write the key into some kind of web interface. To simplify that process I use this handy command:

# Xorg version
cat ~/.ssh/id_rsa.pub | xclip

# Wayland version
cat ~/.ssh/id_rsa.pub | wl-copy

This instantly puts my public key to the clipboard and I can just alt-tab to the browser and paste it to the right field. Simple, isn’t it?