Copy text to the local clipboard from anywhere using the OSC 52 terminal escape sequence. Works over SSH without X11/Wayland forwarding.
echo hello | osc52-copy
osc52-copy "some text"Drop the script somewhere on your $PATH and make it executable:
install -m 755 osc52-copy ~/.local/bin/- A terminal emulator that supports OSC 52 (iTerm2, kitty, WezTerm, recent xterm, Alacritty with
terminal.osc52enabled, etc.). - Standard tools:
bash,base64,tr,printf.
tmux must be configured to forward the sequence to the outer terminal. The script supports either of:
set -g set-clipboard on # preferred; 'external' also works
set -g allow-passthrough on # alternativeIf neither is set, the script prints a warning to stderr explaining how to fix it. With set-clipboard, the script sends a raw OSC 52 sequence. With allow-passthrough, it wraps the sequence in tmux's DCS passthrough envelope.
OSC 52 lets a program tell the terminal "put this base64-encoded text in the clipboard." The script:
- Reads input from stdin or arguments.
- Base64-encodes it (newlines stripped, since OSC sequences cannot contain them).
- Wraps it as
ESC ] 52 ; c ; <base64> BEL(cselects the system clipboard). - Inside tmux, queries
set-clipboardandallow-passthroughto pick the right delivery: raw OSC 52 (set-clipboard) or DCS-wrapped (allow-passthrough). - Writes the sequence to
/dev/ttyso it works even when stdout is piped or redirected.
Why not theimpostor/osc?
osc is a more featureful Go tool (paste, multiple clipboard targets, tty override). osc52-copy is a few lines of bash that fits into dotfiles without an extra binary install.