mirror of
https://github.com/boxpositron/absolute-dotfiles.git
synced 2026-10-24 08:23:19 +00:00
8580db4416
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.
29 lines
1017 B
Bash
Executable File
29 lines
1017 B
Bash
Executable File
#!/usr/bin/env bash
|
|
# pbcopy -- macOS-style clipboard copy for this headless box.
|
|
#
|
|
# There is no X/Wayland display here, so xclip/xsel cannot reach a clipboard.
|
|
# Instead we use OSC 52: the text is handed to the terminal emulator on the
|
|
# other end of the SSH connection, which puts it on YOUR machine's clipboard.
|
|
#
|
|
# ls -la | pbcopy
|
|
# pbcopy < notes.md
|
|
|
|
set -euo pipefail
|
|
|
|
# A real local display (rare here, but free to support).
|
|
if [[ -n "${DISPLAY:-}" ]] && command -v xclip >/dev/null 2>&1; then
|
|
exec xclip -selection clipboard
|
|
fi
|
|
if [[ -n "${WAYLAND_DISPLAY:-}" ]] && command -v wl-copy >/dev/null 2>&1; then
|
|
exec wl-copy
|
|
fi
|
|
|
|
# Inside tmux: load-buffer -w fills the tmux paste buffer and, because
|
|
# set-clipboard is on, forwards OSC 52 out to the terminal emulator.
|
|
if [[ -n "${TMUX:-}" ]] && command -v tmux >/dev/null 2>&1; then
|
|
exec tmux load-buffer -w -
|
|
fi
|
|
|
|
# Bare SSH: emit OSC 52 straight at the controlling terminal.
|
|
{ printf '\033]52;c;'; base64 | tr -d '\n'; printf '\a'; } > /dev/tty
|