When working on a remote server over SSH, copying text to your local clipboard requires extra steps. OSC 52 escape sequences solve this by sending clipboard data through the terminal connection directly to your local machine.

The Problem

On a remote server accessed via SSH, standard clipboard tools like xclip or wl-copy operate on the remote system’s clipboard (if one exists). There’s no direct way to copy file contents or command output to the local machine’s clipboard without manually selecting and copying from the terminal.

OSC 52 Escape Sequences

OSC 52 is an ANSI escape sequence that instructs the terminal emulator to set the system clipboard. The format:

\033]52;c;<base64-encoded-data>\a
  • \033]52; - OSC 52 sequence start
  • c - clipboard selection (primary clipboard)
  • ; - separator
  • <base64-encoded-data> - the clipboard content, base64 encoded
  • \a - sequence terminator (BEL character)

When sent through an SSH connection, the escape sequence passes through to the local terminal emulator, which decodes it and sets the local clipboard.

Terminal Support

Most modern terminal emulators support OSC 52:

TerminalSupport
footYes (default)
AlacrittyYes (default)
kittyYes (requires config)
WezTermYes (default)
iTerm2Yes (requires enabling)
Windows TerminalYes
GNOME TerminalNo

For kitty, add to ~/.config/kitty/kitty.conf:

clipboard_control write-clipboard write-primary

Shell Function

Add this function to ~/.bashrc on the remote server:

# Copy to system clipboard via OSC 52 (works over SSH)
clip() {
  printf '\033]52;c;%s\a' "$(base64 < "${1:-/dev/stdin}")"
}

Reload the shell:

source ~/.bashrc

Usage

Copy a file:

clip file.txt

Copy command output:

cat /etc/hostname | clip
kubectl get pods -o yaml | clip

Copy a string:

echo "text to copy" | clip

tmux Integration

tmux intercepts escape sequences by default. To enable OSC 52 passthrough, add to ~/.tmux.conf:

set -g set-clipboard on

Reload the tmux configuration:

tmux source ~/.tmux.conf

tmux-yank Plugin

The tmux-yank plugin integrates OSC 52 with tmux copy mode, enabling clipboard sync when selecting text.

Install TPM (Tmux Plugin Manager):

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Add to ~/.tmux.conf:

# Enable OSC 52 clipboard
set -g set-clipboard on

# TPM plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-yank'

# tmux-yank settings
set -g @yank_action 'copy-pipe-no-clear'

# Initialize TPM (keep at bottom)
run '~/.tmux/plugins/tpm/tpm'

Reload tmux config and install plugins:

tmux source ~/.tmux.conf

Inside tmux, press prefix + I (capital I) to install plugins.

With tmux-yank installed:

  • Enter copy mode: prefix + [
  • Select text and press y to copy to clipboard
  • prefix + y copies the current command line

Verification

Test the setup by copying something and pasting locally:

echo "OSC 52 test" | clip

Then paste (Ctrl+V or middle-click) in a local application. If the text appears, OSC 52 is working through your SSH connection.

Limitations

  • Data size: Large clipboard contents may be truncated depending on terminal buffer sizes
  • Binary data: Works best with text; binary data needs proper base64 handling
  • Nested SSH: May not work through multiple SSH hops without additional configuration
  • Terminal support: Requires a supporting terminal emulator on the local machine

Summary

OSC 52 escape sequences provide a method to copy text from remote servers directly to the local clipboard over SSH. Combined with tmux-yank, it enables a workflow where selections in tmux copy mode sync to the local system clipboard without additional tools or manual copying.