Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Clipboard selection over remote (ssh) and optionally multi-plexed (tmux) connections. This is primarily from a windows machine using git-for-windows, mintty, and tmux remotely. Untested otherwise.

Other (non-Windows) solutions/recommendations include xclip on the remote end; these will not work unless there is an X-windows server running on the local (Windows) machine.

The remainder is effectively the results of my trial-and-error process.

mintty and OSC-52

Instead, this uses OSC-52 escape sequences. Because they can be considered a security risk (the remote end might be able to see the local end's clipboard values arbitrarily), the capability is disabled by default.

Edit ~/.minttyrc and add the following:

AllowSetSelection=yes

(Restart any mintty sessions.)

tmux

Given that you've updated mintty, using mintty's buffer for copy/paste (defaulting to Ctrl-b [ and Ctrl-b ], respectively, assuming that the tmux prefix is Ctrl-b) should just work.

If it is not working, one thing you might consider is running (via Ctrl-b :):

bind-key -temacs-copy C-q copy-pipe 'base64 | xargs printf "\033]52;c;%s\a"'

(Untested, since even without it things just work for me.)

Emacs

Emacs' copy/paste buffer is a bit different. There is a package clipetty which makes it quite easy. I created a lisp file as ~/.emacs.d/lisp/my-tty.el as:

(require 'clipetty)
; use only one of the following lines
(global-clipetty-mode)
;(global-set-key (kbd "C-c M-w") 'clipetty-kill-ring-save)

If you use the global-clipetty-mode line, then all copies are transferred to the local windows machine; I don't know if this will become annoying and/or a performance hit (if/when selecting large regions), so the second might be preferred. This keybinding should be used instead of the normal M-w, meaning you would do something like:

Move to the start of a region
C-Space
Move to the end of the region
C-c M-w        <--- instead of just M-w

The use of clipetty-kill-ring-save copies the region to both the emacs-specific kill-ring and the OSC-52-based clipboard.

This will work as-is for the first session, but as soon as you disconnect and re-connect with a new ssh TTY (emacs embedded in tmux over ssh), it won't work. According to the clipetty docs (https://github.com/spudlyo/clipetty#dealing-with-a-stale-ssh_tty-environment-variable), add the following to the ~/.tmux.conf file:

set -ag update-environment "SSH_TTY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment