Expand description
The clipboard: an internal register, the system clipboard behind it, and OSC 52 in front of both.
§Why not a clipboard crate
X11 has no clipboard daemon. A selection is owned by a live process, and
when that process exits the content is gone unless a clipboard manager
happened to claim it. xclip and wl-copy fork a background process
specifically to hold that ownership; a library that sets the selection from
inside this process requires this process to stay alive to serve it. For an
editor the failure is: copy, quit, paste elsewhere, get nothing.
Helix reaches for external commands for exactly this reason, citing Neovim’s
provider/clipboard.vim; oh-my-pi does the same and keeps a PowerShell path
on Windows. Three implementations agreeing is enough evidence.
§The layers
- The register. Always present, always the source of truth for paste inside TYPE. Nothing can make this fail.
- OSC 52 on write, emitted first. An escape sequence carrying base64 that the local terminal intercepts, so a copy over SSH lands in the laptop’s clipboard rather than the server’s.
- A command provider, chosen by environment variable and binary presence rather than by trying and catching.
§Reading
There is no OSC 52 read. The reply has to be parsed off the input stream, and terminals disable clipboard reads by default for good reason — a remote host that can read your clipboard reads whatever you last copied. Reads go to the provider, then fall back to the register.
§Failure
Nothing here surfaces an error. A headless box with no clipboard is a normal condition, not something to interrupt someone about.
Functions§
- enable_
system - Let the clipboard reach the system. Called once, by the binary.
- get
- Paste: the system provider, falling back to the register.
- provider_
name - Which clipboard provider was chosen, for a log line.
- register
- What the register holds.
- set
- Copy: register, then OSC 52, then the system provider.
- set_
register - Set the register alone, touching nothing outside this process.