The Linux Clipboard Explained: Wayland, X11, and Why Copied Text Disappears

The Linux Clipboard Explained: Wayland, X11, and Why Copied Text Disappears

Linux has two clipboards, and knowing which is which explains most clipboard confusion.

CLIPBOARD is the conventional one. Ctrl+C copies, Ctrl+V pastes.

PRIMARY holds whatever you last selected with the mouse, with no copy step at all. Middle-click pastes it.

They are entirely independent. Highlighting text puts it in PRIMARY without touching CLIPBOARD, which is why middle-click can paste something completely different from what Ctrl+V gives you.

PRIMARY is genuinely useful once you are used to it: select a word in one window, middle-click in another, done. It also means a stray selection silently overwrites what middle-click will paste.

There is a third, SECONDARY, which essentially nothing uses.

Why copied text disappears

The clipboard is not a buffer somewhere in the system. There is no clipboard process holding your data.

When you copy, the application declares itself the owner of the selection. When you paste, the target asks the display server who owns it, and the display server asks that application to send the data.

So closing the source application removes the only copy. Copying from a text editor, closing it, and finding nothing to paste is the system working as designed.

This also explains why pasting a large image can be slow: the transfer happens at paste time, not at copy time.

A clipboard manager fixes this by taking ownership itself whenever the selection changes, so the content survives the source closing.

Wayland tools

sudo apt install wl-clipboard
echo "hello" | wl-copy
wl-paste

wl-copy --primary < file.txt     # PRIMARY selection
wl-paste --primary

wl-copy --type text/plain "some text"
wl-paste --list-types            # what formats are on offer
wl-copy --clear

wl-paste --list-types is the diagnostic command when a paste is not working. It shows exactly what the source offered:

wl-paste --list-types
# text/html
# text/plain;charset=utf-8
# image/png

If the target only accepts text/plain and the source offered only image/png, nothing happens and neither application reports an error.

Watch for changes:

wl-paste --watch cat

That is the basis of a minimal clipboard history script.

X11 tools

sudo apt install xclip xsel
echo "hello" | xclip -selection clipboard
xclip -selection clipboard -o

echo "hello" | xsel --clipboard --input
xsel --clipboard --output

xclip without -selection clipboard uses PRIMARY, which is the default and is not what most people expect. A script that appears to copy nothing is usually this.

Note the asymmetry: wl-copy defaults to CLIPBOARD, xclip defaults to PRIMARY. A script ported between them behaves differently.

Writing scripts that work in both

copy() {
    if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
        wl-copy
    else
        xclip -selection clipboard
    fi
}

echo "hello" | copy

Or detect by what is installed:

if command -v wl-copy >/dev/null && [ -n "$WAYLAND_DISPLAY" ]; then
    CLIP_COPY="wl-copy"
    CLIP_PASTE="wl-paste"
else
    CLIP_COPY="xclip -selection clipboard"
    CLIP_PASTE="xclip -selection clipboard -o"
fi

Useful in practice:

pwd | wl-copy                              # path to clipboard
wl-paste > pasted.txt                      # clipboard to file
history | tail -1 | wl-copy                # last command
openssl rand -base64 24 | wl-copy          # generated password

Clipboard managers

cliphist for wlroots compositors:

wl-paste --watch cliphist store &
cliphist list | fuzzel --dmenu | cliphist decode | wl-copy

clipman is a similar option for Sway and Hyprland.

GNOME and KDE have managers built in. Klipper on KDE is long-standing and configurable; GNOME’s is minimal and usually supplemented with an extension.

CopyQ works across desktops and both session types, with a searchable history, tagging, and scripting. The heaviest option and the most capable.

One security consideration: a clipboard manager stores everything you copy, including passwords out of a password manager. Most offer exclusion rules or a private mode, and configuring that is worth the two minutes.

# CopyQ: ignore anything from a password manager window
# configured through its interface

Our pass guide covers the clipboard timeout approach, where the entry is cleared automatically after a short interval.

Wayland and XWayland

Wayland clients and XWayland clients use different selection mechanisms. The compositor bridges them, and the bridging is imperfect.

Symptoms: copying from an X11 application into a Wayland one fails, or the reverse, or it works for text but not for images.

echo $XDG_SESSION_TYPE           # wayland or x11
xlsclients                        # which X11 clients are running

Most compositors handle this well now. Where it fails, it is worth checking whether the problematic application is running under XWayland, since forcing it to native Wayland frequently resolves the problem.

For Electron applications:

myapp --enable-features=UseOzonePlatform --ozone-platform=wayland

Our Wayland and X11 explainer covers the architectural difference.

Terminals

Ctrl+Shift+C and Ctrl+Shift+V in most terminals, because Ctrl+C sends SIGINT and has done for decades. Our process signals guide covers what it is actually doing.

Middle-click pastes PRIMARY.

Bracketed paste is worth understanding. It wraps pasted text in escape sequences so the shell knows it was pasted rather than typed, which means a pasted multi-line block does not execute line by line as it arrives.

# in ~/.inputrc
set enable-bracketed-paste on

Most modern shells and terminals enable it by default, and it is the mechanism that protects you from the attack below.

Pasting commands from websites

A real risk, and a simple one.

CSS and JavaScript can make the text you copy differ from the text displayed. A page can show:

sudo apt install package

while what lands on your clipboard is that plus a hidden command, plus a trailing newline that causes immediate execution before you can read it.

Defences:

Paste into an editor first for anything from a source you do not fully trust.

Keep bracketed paste enabled, which prevents the trailing newline executing the command automatically.

Read what actually pasted, not what you expected.

wl-paste | cat -A | head    # show every character including hidden ones

cat -A reveals control characters and line endings, which is the definitive check.

Frequently Asked Questions

Why does Linux have two clipboards?

The CLIPBOARD selection is the conventional one used by Ctrl+C and Ctrl+V. The PRIMARY selection holds whatever text you last highlighted and pastes with the middle mouse button, with no explicit copy step. They are independent, which is why middle-click can paste something different from Ctrl+V.

Why does copied text disappear when I close the application?

The clipboard is not storage. The source application holds the data and transfers it on request, so closing it removes the only copy. A clipboard manager solves this by taking ownership of the selection itself and keeping the content.

How do I copy to the clipboard from a script on Wayland?

Pipe into wl-copy from the wl-clipboard package, and read back with wl-paste. On X11 the equivalents are xclip or xsel, and using the wrong pair for your session type is a common source of confusion when a script works in one environment and not another.

Why can I not paste into a different application on Wayland?

Usually a mismatch between Wayland and XWayland clients, since each has its own selection mechanism and the compositor bridges them imperfectly. It can also be that the source offered only a format the target does not accept, which wl-paste —list-types will reveal.

How do I paste into a terminal?

Ctrl+Shift+V in most terminals, because plain Ctrl+V has historically meant something else in a terminal context. Middle-click pastes the PRIMARY selection, and many terminals also let you rebind these if the default is inconvenient.

Is it safe to paste commands from a website into a terminal?

Not always. CSS and JavaScript can make the copied text differ from what is displayed, including hiding a trailing newline that causes the command to execute on paste rather than waiting. Paste into a text editor first, or use a terminal with bracketed paste, which most modern ones enable by default.