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.
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.
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=clipcopyReload:
source ~/.zshrcTest:
echo "hello from linux" | clipcopyNow paste in Windows. You should see:
hello from linux
Useful examples:
pwd | clipcopy
cat notes.txt | clipcopy
git diff | clipcopyUse 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
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:
pbpasteor 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.
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 → Linuxand 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