Ssh – Vim losing ability to copy to client clipoard over SSH

clipboardsshtmuxvimx11

I have setup vim to be able to yank text from the server instance of vim to the clipboard on My client's operating system (through the SSH connection).
The basic setup is

  • Client side
    • gnome-terminal
  • Serverside
    • SSH with X11 session forwarding e.g. ssh -Y <user>@<server>
      • Tmux
        • vim-gtk

vim version running on server – vim-gtk with extras necessary to copy to the system clipboard

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan  2 2014 19:40:46)
Included patches: 1-52
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by buildd@
Huge version with GTK2 GUI.  Features included (+) or not (-):
  • Operating System Ubuntu 14.04

Inside vim (running on server) I can visual select text – then hit ", +, y and I will then have that visual selection available back on My client OS clipboard.
This works when I connect to the server and start a new tmux session, but when I reconnect to the server after the SSH has broken and reattach to tmux session it stops working.
Any ideas why this could be?

Best Answer

This is my solution (thanks for the xpra pointer). It goes like this:

  1. Assumes same bashrc locally and remote
  2. Use tmux-ssh to attach to the remote site. It takes normal ssh(1) args like -p and -u. It basically calls tmux-x-attach() on the remote site
  3. tmux-x-attach() starts an xpra server if none is running, then attaches to the running xpra server and submits to tmux-attach()
  4. tmux-attach() will either
    1. start a new tmux session if none exists
    2. attach to the (single) running tmux instance
    3. prompt for which of multiple tmux sessions to attach to
  5. when detaching, we fall back into tmux-x-attach, which detaches from xpra before the ssh session terminates
  6. An attempt (tput init) is made to clean up the terminal if tmux was detached from within eg. vim

From .bashrc

  tmux-x-attach() {
     ps -f -u $USER | grep -v grep | grep -q 'xpra start' || xpra start :9
     xpra attach :9 --opengl=no > /tmp/xpra-attach.log 2>&1 &
     DISPLAY=:9 tmux-attach "$@"
     xpra detach :9
  }

  tmux-attach() {
     case $(tmux list-sessions 2>/dev/null | wc -l) in
        0) tmux ;;
        1) tmux attach ;;
        *)
           tmux list-sessions 
           read -n 1 -p "Select command: " N < /dev/tty > /dev/tty;
           tmux attach -t $N
           ;;
     esac
  }
  tmux-ssh() { ssh "$@" -A -X -t 'PS1=tmux-ssh- ; . ~/.bashrc ; tmux-x-attach'; tput init; }