Skip to content

Instantly share code, notes, and snippets.

@SmileMachine
Last active March 3, 2026 01:16
Show Gist options
  • Select an option

  • Save SmileMachine/d8e9978c50f38173a0bda4fbe55a1783 to your computer and use it in GitHub Desktop.

Select an option

Save SmileMachine/d8e9978c50f38173a0bda4fbe55a1783 to your computer and use it in GitHub Desktop.
Copy remote(ssh) text to local clipboard (like macOS pbcopy).

pbcopy

Copy text to your local clipboard over SSH, just like macOS's built-in pbcopy.

echo "hello" | pbcopy
cat file.txt | pbcopy
pbcopy "some text"

Requirements

  • Local terminal that supports OSC 52: iTerm2, Alacritty, Kitty, WezTerm
  • tmux (optional): version 2.6+, with the following line in ~/.tmux.conf:
    set -g allow-passthrough on
    

Installation

Download the script and place it in ~/.local/bin/:

curl -o ~/.local/bin/pbcopy https://gist.githubusercontent.com/SmileMachine/d8e9978c50f38173a0bda4fbe55a1783/raw/pbcopy
chmod +x ~/.local/bin/pbcopy

Make sure ~/.local/bin is in your PATH. If not, add this to your ~/.bashrc or ~/.zshrc:

export PATH="$HOME/.local/bin:$PATH"

Notes

  • Uses the OSC 52 terminal escape sequence. The sequence travels back through SSH and is interpreted by your local terminal, which writes the content to the clipboard.
  • Inside tmux, the sequence is wrapped in a DCS passthrough so tmux forwards it to the outer terminal instead of consuming it.
  • Very large inputs may be silently truncated depending on your terminal (~1 MB).
#!/usr/bin/env bash
#
# pbcopy - Copy text to your local clipboard over SSH
# https://gist.github.com/SmileMachine/d8e9978c50f38173a0bda4fbe55a1783
_b64() { base64 -w0 2>/dev/null || base64 | tr -d '\n'; }
if [ $# -gt 0 ]; then
data=$(printf '%s' "$*" | _b64)
else
data=$(_b64)
fi
if [[ -n "$TMUX" ]]; then
printf '\ePtmux;\e\e]52;c;%s\007\e\\' "$data" > /dev/tty
else
printf '\e]52;c;%s\007' "$data" > /dev/tty
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment