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:

Terminal window
echo $XDG_SESSION_TYPE
  • Returns x11 → Use xclip or xsel
  • Returns wayland → Use wl-clipboard

X11 Clipboard Tools (xclip & xsel)

Installation

Terminal window
sudo apt-get install xclip
sudo apt-get install xsel

Understanding 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

Terminal window
# Copy command output to clipboard (Ctrl+V compatible)
command | xclip -selection clipboard
command | xclip -sel clip # Shorthand
command | xclip -se c # Even shorter
# Copy file contents
cat myfile.txt | xclip -sel clip
xclip -sel clip < myfile.txt
# Examples
pwd | xclip -sel clip
ifconfig | xclip -selection clipboard
# Paste from clipboard
xclip -selection clipboard -o
xclip -sel clip -o

xsel Usage

xsel offers simpler syntax with short flags:

Terminal window
# Copy to clipboard (Ctrl+V compatible)
command | xsel -b # -b = --clipboard
command | xsel --clipboard
# Copy string directly
xsel -b <<< "text to copy"
# Paste from clipboard
xsel -b -o
xsel --clipboard --output
# Primary selection (middle-click paste)
command | xsel -p # -p = --primary

Useful Aliases

Add to your ~/.bashrc or ~/.zshrc:

Terminal window
# xclip aliases
alias cclip='xclip -selection clipboard'
alias clipp='xclip -selection clipboard -o'
# xsel aliases
alias cb='xsel -b'
alias cbp='xsel -b -o'

Wayland Clipboard Tools (wl-clipboard)

Installation

Terminal window
sudo apt install wl-clipboard

Basic Usage

wl-clipboard provides wl-copy and wl-paste commands:

Terminal window
# Copy text
wl-copy "Hello world!"
# Copy command output
ls ~/Downloads | wl-copy
# Copy file contents
wl-copy < ~/Pictures/photo.png
cat file.txt | wl-copy
# Paste to stdout
wl-paste
# Paste to file
wl-paste > clipboard.txt
# Process clipboard data
wl-paste | sort | wl-copy

Key Options

OptionDescription
-p, --primaryUse primary selection instead of clipboard
-f, --forkFork to background (keeps data after process exits)
-c, --clearClear the clipboard
-t, --type <mime>Specify MIME type (e.g., text/html, image/png)
-n, --no-newlineDon’t append newline when pasting

Watch Mode (Clipboard Managers)

Terminal window
# Monitor clipboard changes and store history
wl-paste --watch cliphist store

Add to Sway/Hyprland config to run at startup.

Important Wayland Considerations

  1. Clipboard Persistence: Data is cleared when the source application closes
  2. Solution: Use wl-clip-persist to preserve clipboard contents
  3. Neovim Integration: Automatically uses wl-copy when installed (no config needed)

Quick Reference

TaskX11 (xclip)X11 (xsel)Wayland (wl-clipboard)
Copycmd | xclip -sel clipcmd | xsel -bcmd | wl-copy
Pastexclip -sel clip -oxsel -b -owl-paste
Copy filexclip -sel clip < filexsel -b < filewl-copy < file
ClearN/Axsel -b -cwl-copy -c

Common Use Cases

Copy command output

Terminal window
# X11
ls -la | xclip -sel clip
# Wayland
ls -la | wl-copy

Copy file contents

Terminal window
# X11
cat config.json | xclip -sel clip
# Wayland
cat config.json | wl-copy

Copy current directory path

Terminal window
# X11
pwd | xclip -sel clip
# Wayland
pwd | wl-copy

Save clipboard to file

Terminal window
# X11
xclip -sel clip -o > saved.txt
# Wayland
wl-paste > saved.txt

Sources

  1. How To Copy Command Output To Linux Clipboard Directly - nixCraft
  2. How can I copy the output of a command directly into my clipboard? - Stack Overflow
  3. A command-line clipboard copy and paste utility? - Ask Ubuntu
  4. Copy and paste at the Linux command line with xclip - Opensource.com
  5. xclip reference: copy-to-clipboard CLI - The Eclectic Coder
  6. GitHub - bugaevc/wl-clipboard: Command-line copy/paste utilities for Wayland
  7. wl-clipboard - Wayland copy and paste command line utilities - Ubuntu Manpage
  8. How to copy text to the clipboard when using Wayland? - Super User
  9. Clipboard Managers - Hyprland Wiki