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)
# Copyecho "my secret key" | xclip -selection clipboard
# Paste (pointless but works)xclip -selection clipboard -omy secret keyThe -selection clipboard is important. X11 has three clipboards:
primary— what you selected with mouseclipboard— what you Ctrl+C’dsecondary— rarely used
For “normal” copy-paste, use clipboard. Most tools expect it.
Shorthand:
# Works the same (clipboard is default)cat /etc/hosts | xclipxsel (X11 Alternative)
xsel is older, sometimes pre-installed:
# Copyecho "data" | xsel --clipboard --input
# Pastexsel --clipboard --output
# Clearxsel --clipboard --clearHonestly, xclip is more intuitive. Use it if available.
wl-copy and wl-paste (Wayland)
Modern systems use Wayland. If xclip doesn’t work, try:
# Copyecho "secret" | wl-copy
# Pastewl-pastesecretNo flags needed. Clean and simple.
Which Do You Have?
# Check for Waylandecho $WAYLAND_DISPLAY/run/user/1000/wayland-0
# Check for X11echo $DISPLAY:0Both? Try wl-copy first, fall back to xclip:
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}
# Usageecho "hello" | copypastehelloAdd 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:
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# Or with wl-copycat ~/.ssh/id_ed25519.pub | wl-copyNow Ctrl+V in your browser. Magic.
Remote SSH: Doesn’t Work Natively
SSH into a server:
ssh user@serveruser@server$ echo "secret" | xclip# ✗ Error: Can't open displayWhy? The display/Wayland socket isn’t forwarded. Options:
1. Use SSH agent + local clipboard wrapper
# On your local machine, create a wrapper#!/bin/bash# Copies from stdin to local clipboard over SSHxclip -selection clipboardThen on the remote, pipe to:
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:
# In tmux: bind to copy to local clipboardbind-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:
# From your local machine, copy your public key to the serverssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# Now SSH without a passwordssh user@serverMuch better than clipboard gymnastics.
pbcopy on Linux (Make It Work)
Some scripts expect pbcopy. Create an alias:
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'fiNow scripts expecting macOS pbcopy will work on Linux.
Install Missing Tools
# Debian/Ubuntusudo apt install xclip xsel
# Fedorasudo dnf install xclip xsel wl-clipboard
# Archsudo pacman -S xclip wl-clipboardBottom Line
- Local X11:
echo "stuff" | xclip -selection clipboard - Local Wayland:
echo "stuff" | wl-copy - Remote SSH: Don’t bother. Use
ssh-copy-idor other methods. - tmux: Configure copy-pipe to hit local clipboard even over SSH.
Simple. No mouse required.
Clipboard in Scripts
You can use clipboard tools in scripts too:
# Copy command output to clipboardps aux | grep nginx | wl-copy # Waylandps aux | grep nginx | xclip -selection clipboard # X11
# Read from clipboard (pipe it into a command)wl-paste | grep "error" | head -5xclip -selection clipboard -o | wc -lUseful for quick one-liners when you want to paste a result somewhere without scrolling through terminal output.