Skip to content
Go back

Using the Clipboard from the Linux Terminal

By SumGuy 4 min read
Using the Clipboard from the Linux Terminal

You SSH into a server. You generate an SSH key. Now what? Ctrl+C doesn’t work. There’s no mouse. How do you get that key into your clipboard?

Welcome to terminal clipboard hell. Except it’s not hell—it’s actually straightforward once you know the tools.

The Players

X11 (older): Uses xclip or xsel. Still dominant on desktop Linux.

Wayland (newer): Uses wl-copy and wl-paste. Growing adoption.

macOS: Uses pbcopy and pbpaste. Not relevant here, but handy to know.

SSH/remote: Need to forward clipboard or use tmux integration tricks.

xclip (X11)

Terminal window
# Copy
echo "my secret key" | xclip -selection clipboard
# Paste (pointless but works)
xclip -selection clipboard -o
my secret key

The -selection clipboard is important. X11 has three clipboards:

For “normal” copy-paste, use clipboard. Most tools expect it.

Shorthand:

Terminal window
# Works the same (clipboard is default)
cat /etc/hosts | xclip

xsel (X11 Alternative)

xsel is older, sometimes pre-installed:

Terminal window
# Copy
echo "data" | xsel --clipboard --input
# Paste
xsel --clipboard --output
# Clear
xsel --clipboard --clear

Honestly, xclip is more intuitive. Use it if available.

wl-copy and wl-paste (Wayland)

Modern systems use Wayland. If xclip doesn’t work, try:

Terminal window
# Copy
echo "secret" | wl-copy
# Paste
wl-paste
secret

No flags needed. Clean and simple.

Which Do You Have?

Terminal window
# Check for Wayland
echo $WAYLAND_DISPLAY
/run/user/1000/wayland-0
# Check for X11
echo $DISPLAY
:0

Both? Try wl-copy first, fall back to xclip:

Terminal window
copy() {
if command -v wl-copy &> /dev/null; then
wl-copy
elif command -v xclip &> /dev/null; then
xclip -selection clipboard
else
echo "No clipboard tool found" >&2
return 1
fi
}
paste() {
if command -v wl-paste &> /dev/null; then
wl-paste
elif command -v xsel &> /dev/null; then
xsel --clipboard --output
else
echo "No clipboard tool found" >&2
return 1
fi
}
# Usage
echo "hello" | copy
paste
hello

Add to your .bashrc or .zshrc.

Copy Your SSH Key (The Classic Move)

You just generated a key on a server and need to paste it into GitHub:

Terminal window
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# Or with wl-copy
cat ~/.ssh/id_ed25519.pub | wl-copy

Now Ctrl+V in your browser. Magic.

Remote SSH: Doesn’t Work Natively

SSH into a server:

Terminal window
ssh user@server
user@server$ echo "secret" | xclip
# ✗ Error: Can't open display

Why? The display/Wayland socket isn’t forwarded. Options:

1. Use SSH agent + local clipboard wrapper

~/.local/bin/remote-copy
# On your local machine, create a wrapper
#!/bin/bash
# Copies from stdin to local clipboard over SSH
xclip -selection clipboard

Then on the remote, pipe to:

Terminal window
remote$ echo "secret" | ssh -T local-user@local-machine 'cat | xclip -selection clipboard'

Awkward. Not recommended.

2. Use tmux (Better)

If you’re using tmux locally, tmux can access your clipboard:

Terminal window
# In tmux: bind to copy to local clipboard
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"

Now when you select text in tmux and press y, it goes to your local clipboard. Works over SSH too!

3. Just don’t use clipboard

Honestly? Copy-pasting keys is overrated. Use ssh-copy-id:

Terminal window
# From your local machine, copy your public key to the server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# Now SSH without a password
ssh user@server

Much better than clipboard gymnastics.

pbcopy on Linux (Make It Work)

Some scripts expect pbcopy. Create an alias:

~/.bashrc
if command -v xclip &> /dev/null; then
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
elif command -v wl-copy &> /dev/null; then
alias pbcopy='wl-copy'
alias pbpaste='wl-paste'
fi

Now scripts expecting macOS pbcopy will work on Linux.

Install Missing Tools

Terminal window
# Debian/Ubuntu
sudo apt install xclip xsel
# Fedora
sudo dnf install xclip xsel wl-clipboard
# Arch
sudo pacman -S xclip wl-clipboard

Bottom Line

Simple. No mouse required.

Clipboard in Scripts

You can use clipboard tools in scripts too:

Terminal window
# Copy command output to clipboard
ps aux | grep nginx | wl-copy # Wayland
ps aux | grep nginx | xclip -selection clipboard # X11
# Read from clipboard (pipe it into a command)
wl-paste | grep "error" | head -5
xclip -selection clipboard -o | wc -l

Useful for quick one-liners when you want to paste a result somewhere without scrolling through terminal output.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it may appear here.


Previous Post
Docker Container Labels: The Metadata You're Ignoring
Next Post
Why Docker Builds Are Slow: Layer Cache Explained

Related Posts