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?

  • 1
    I imagine the system clipboard is accessed over X, so when you disconnect the X connection vim is using is lost. I'm not sure there's a way around that (except restarting vim). – Michael Homer Dec 26 '15 at 8:40
  • Actually: xpra is "screen for X" which maybe you could use in addition to tmux, even though you wouldn't be using any actual windows for anything. I'm not certain that would work, but perhaps worth a try. – Michael Homer Dec 26 '15 at 8:44
  • @MichaelHomer Ah, I just tested a vim restart and that works! its not a complete fix but at least narrows down whats happening – the_velour_fog Dec 26 '15 at 8:44
  • 1
    Ok, so that means the problem is what I said in the first comment - the second comment is the only workaround I can think of and I'm not sure it will work. Restarting might be the only way, unless vim has some internal support for updating its environment and reinitialising its X11 connection. – Michael Homer Dec 26 '15 at 8:46
  • 2
    ssh -Y tunnels X11 over the SSH connection. When the connection closes, so does the X connection and client-side forwarder, but vim still thinks that's what it's meant to be talking to and it loses its session (like terminating your local X server). Restarting vim within the same environment isn't even a general solution on its own if this is a multi-user system, because you won't necessarily get the same DISPLAY each time. – Michael Homer Dec 26 '15 at 8:52

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
  tmux-ssh() { ssh "$@" -A -X -t 'PS1=tmux-ssh- ; . ~/.bashrc ; tmux-x-attach'; tput init; }
| improve this answer | |
  • care to elaborate a little more about your solution? Is it a solution for using ssh to connect to remote server and using tmux in that remote server? how to use the function to solve the issue? – Aaron Shen Sep 10 '16 at 8:50
  • @aaron Hope the elaborated answer makes more sense .. – TheMadsen Sep 11 '16 at 18:19
  • @the_velour_fog: Please upvote if you find the the answer useful. It /does/ solve the problem you describe .. – TheMadsen Jun 11 '19 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.