feat(bin): add pbcopy/pbpaste shims for headless clipboard over OSC 52

Headless servers have no DISPLAY or WAYLAND_DISPLAY, so xclip and xsel
cannot reach a clipboard even when installed. These shims use OSC 52 to
hand text to the terminal emulator on the other end of the SSH connection,
which puts it on the local machine's clipboard.

Inside tmux, pbcopy uses `tmux load-buffer -w -` so the text lands in the
tmux paste buffer and is forwarded outward (requires `set-clipboard on`).
Outside tmux it writes the escape sequence to /dev/tty directly. Both fall
back to xclip/wl-copy when a real display is present.

pbpaste reads the tmux buffer only; terminals refuse OSC 52 reads, since
that would let any remote program siphon the user's clipboard.
This commit is contained in:
David Ibia
2026-08-28 21:47:40 +00:00
parent 25d06b4ce4
commit 8580db4416
2 changed files with 50 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# pbpaste -- companion to pbcopy.
#
# Terminals almost universally refuse OSC 52 *reads* (it would let any remote
# program siphon your clipboard), so this cannot see your local clipboard.
# What it can do is echo back the tmux paste buffer, i.e. whatever pbcopy or
# a tmux copy-mode yank last put there.
set -euo pipefail
if [[ -n "${DISPLAY:-}" ]] && command -v xclip >/dev/null 2>&1; then
exec xclip -selection clipboard -o
fi
if [[ -n "${WAYLAND_DISPLAY:-}" ]] && command -v wl-paste >/dev/null 2>&1; then
exec wl-paste
fi
if [[ -n "${TMUX:-}" ]] && command -v tmux >/dev/null 2>&1; then
exec tmux save-buffer -
fi
echo "pbpaste: no readable clipboard (not in tmux, no display)" >&2
exit 1