Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jclosure/7292a2446eb1fc945edda60c64a9c22b to your computer and use it in GitHub Desktop.

Select an option

Save jclosure/7292a2446eb1fc945edda60c64a9c22b to your computer and use it in GitHub Desktop.
Shared clipboard over SSH: Windows Terminal ⇄ Linux zsh/Emacs via OSC 52

Shared Clipboard over SSH: Windows Terminal ⇄ Linux zsh/Emacs

Goal: use the Windows clipboard from a remote Linux server over SSH, especially from zsh and terminal Emacs.

This starts with the cleanest portable path: OSC 52.

Concept

OSC 52 is a terminal escape sequence that lets a remote program ask the local terminal to write text into the local clipboard.

Linux shell / Emacs
   ↓ OSC 52 escape sequence over SSH
Windows Terminal
   ↓
Windows clipboard

This gives reliable remote → Windows clipboard support without socket daemons or OS-specific clipboard tools on the server.


1. zsh: copy from Linux to Windows clipboard

On the Linux server, add this to ~/.zshrc:

clipcopy() {
  perl -MMIME::Base64 -0777 -ne 'print "\e]52;c;" . encode_base64($_, "") . "\a"'
}

alias pbcopy=clipcopy
alias winclip=clipcopy

Reload:

source ~/.zshrc

Test:

echo "hello from linux" | clipcopy

Now paste in Windows. You should see:

hello from linux

Useful examples:

pwd | clipcopy
cat notes.txt | clipcopy
git diff | clipcopy

2. Emacs: copy from terminal Emacs to Windows clipboard

Use the Emacs package clipetty, which hooks Emacs copy/kill operations into OSC 52.

With use-package:

(use-package clipetty
  :ensure t
  :hook (after-init . global-clipetty-mode))

Or manually:

(package-install 'clipetty)
(require 'clipetty)
(global-clipetty-mode 1)

Now copying/killing text in terminal Emacs updates the Windows clipboard.

Emacs kill-ring
   ↓
clipetty
   ↓ OSC 52
Windows Terminal
   ↓
Windows clipboard

3. Windows clipboard back into Linux

For normal interactive use, just paste through Windows Terminal:

  • In zsh, paste normally with Windows Terminal.
  • In terminal Emacs, paste normally into the buffer.

That covers most practical workflows.

Programmatic paste, such as this:

pbpaste

or Emacs pulling directly from the Windows clipboard with interprogram-paste-function, usually needs a separate clipboard agent because OSC 52 clipboard read support is inconsistent and restricted for security.


4. Full bidirectional version

For true programmatic copy/paste, use this architecture:

Linux zsh / Emacs
   ↓ localhost clipboard client
SSH reverse tunnel
   ↓
Windows clipboard agent
   ↓
Windows clipboard

Then the Linux side can expose:

pbcopy   # Linux → Windows clipboard
pbpaste  # Windows clipboard → Linux

and Emacs can use:

(setq interprogram-cut-function ...)
(setq interprogram-paste-function ...)

This is the same core idea as older nc/pbcopy gists, but cleaned up:

  • transport is separate from clipboard backend
  • Windows/macOS/Linux backends are pluggable
  • protocol can support typed MIME payloads later
  • OSC 52 remains the zero-config fast path for text copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment