README
Purpose
Guide for copying to and pasting from the system clipboard using command-line utilities on Ubuntu Linux. Covers both X11 (xclip, xsel) and Wayland (wl-clipboard) display servers.
Display Server Detection
First, determine which display server you’re using:
echo $XDG_SESSION_TYPE- Returns
x11→ Use xclip or xsel - Returns
wayland→ Use wl-clipboard
X11 Clipboard Tools (xclip & xsel)
Installation
sudo apt-get install xclipsudo apt-get install xselUnderstanding X Selections
X11 maintains 3 separate clipboards:
- PRIMARY: Middle-click paste (xclip default)
- SECONDARY: Rarely used
- CLIPBOARD: Ctrl+C/Ctrl+V (most common)
xclip Usage
# Copy command output to clipboard (Ctrl+V compatible)command | xclip -selection clipboardcommand | xclip -sel clip # Shorthandcommand | xclip -se c # Even shorter
# Copy file contentscat myfile.txt | xclip -sel clipxclip -sel clip < myfile.txt
# Examplespwd | xclip -sel clipifconfig | xclip -selection clipboard
# Paste from clipboardxclip -selection clipboard -oxclip -sel clip -oxsel Usage
xsel offers simpler syntax with short flags:
# Copy to clipboard (Ctrl+V compatible)command | xsel -b # -b = --clipboardcommand | xsel --clipboard
# Copy string directlyxsel -b <<< "text to copy"
# Paste from clipboardxsel -b -oxsel --clipboard --output
# Primary selection (middle-click paste)command | xsel -p # -p = --primaryUseful Aliases
Add to your ~/.bashrc or ~/.zshrc:
# xclip aliasesalias cclip='xclip -selection clipboard'alias clipp='xclip -selection clipboard -o'
# xsel aliasesalias cb='xsel -b'alias cbp='xsel -b -o'Wayland Clipboard Tools (wl-clipboard)
Installation
sudo apt install wl-clipboardBasic Usage
wl-clipboard provides wl-copy and wl-paste commands:
# Copy textwl-copy "Hello world!"
# Copy command outputls ~/Downloads | wl-copy
# Copy file contentswl-copy < ~/Pictures/photo.pngcat file.txt | wl-copy
# Paste to stdoutwl-paste
# Paste to filewl-paste > clipboard.txt
# Process clipboard datawl-paste | sort | wl-copyKey Options
| Option | Description |
|---|---|
-p, --primary | Use primary selection instead of clipboard |
-f, --fork | Fork to background (keeps data after process exits) |
-c, --clear | Clear the clipboard |
-t, --type <mime> | Specify MIME type (e.g., text/html, image/png) |
-n, --no-newline | Don’t append newline when pasting |
Watch Mode (Clipboard Managers)
# Monitor clipboard changes and store historywl-paste --watch cliphist storeAdd to Sway/Hyprland config to run at startup.
Important Wayland Considerations
- Clipboard Persistence: Data is cleared when the source application closes
- Solution: Use
wl-clip-persistto preserve clipboard contents - Neovim Integration: Automatically uses wl-copy when installed (no config needed)
Quick Reference
| Task | X11 (xclip) | X11 (xsel) | Wayland (wl-clipboard) |
|---|---|---|---|
| Copy | cmd | xclip -sel clip | cmd | xsel -b | cmd | wl-copy |
| Paste | xclip -sel clip -o | xsel -b -o | wl-paste |
| Copy file | xclip -sel clip < file | xsel -b < file | wl-copy < file |
| Clear | N/A | xsel -b -c | wl-copy -c |
Common Use Cases
Copy command output
# X11ls -la | xclip -sel clip
# Waylandls -la | wl-copyCopy file contents
# X11cat config.json | xclip -sel clip
# Waylandcat config.json | wl-copyCopy current directory path
# X11pwd | xclip -sel clip
# Waylandpwd | wl-copySave clipboard to file
# X11xclip -sel clip -o > saved.txt
# Waylandwl-paste > saved.txtSources
- How To Copy Command Output To Linux Clipboard Directly - nixCraft
- How can I copy the output of a command directly into my clipboard? - Stack Overflow
- A command-line clipboard copy and paste utility? - Ask Ubuntu
- Copy and paste at the Linux command line with xclip - Opensource.com
- xclip reference: copy-to-clipboard CLI - The Eclectic Coder
- GitHub - bugaevc/wl-clipboard: Command-line copy/paste utilities for Wayland
- wl-clipboard - Wayland copy and paste command line utilities - Ubuntu Manpage
- How to copy text to the clipboard when using Wayland? - Super User
- Clipboard Managers - Hyprland Wiki
Related Research
- ../ubuntu-audio-transcription/ - Uses Ubuntu command-line tools for audio processing