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:
# Uses primary selection (not clipboard) — paste with middle-clickcat /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 wl-clipboard
# 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.
Common Questions
How do I copy my SSH public key to the clipboard from the terminal?
Pipe the public key file into your clipboard tool. On X11, run cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard. On Wayland, run cat ~/.ssh/id_ed25519.pub | wl-copy. Then press Ctrl+V in your browser.
Should I use wl-copy, xclip, or xsel?
Use wl-copy on Wayland and xclip on X11. The wl-copy command needs no flags, while xclip needs -selection clipboard for normal Ctrl+V pasting. The xsel tool also works on X11 and is sometimes pre-installed, but xclip is more intuitive. If a system runs both display servers, try wl-copy first and fall back to xclip.
Why does xclip say “Can’t open display” over SSH?
The remote session has no forwarded X11 display or Wayland socket, so xclip has nothing to connect to. Two workarounds exist. In local tmux, bind a copy-mode key to copy-pipe-and-cancel with xclip -selection clipboard, which sends the selection to your local clipboard. Selecting text in tmux and pressing y then reaches that local clipboard even over SSH. Or skip the clipboard and run ssh-copy-id from your local machine to install your public key.
What is the difference between primary and clipboard in xclip?
X11 keeps separate buffers. The primary selection holds whatever you selected with the mouse, and you paste it with middle-click. The clipboard selection holds what you copied with Ctrl+C. Plain xclip uses primary, so pass -selection clipboard for normal copy-paste.