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 [email protected]
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). Dec 26, 2015 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. Dec 26, 2015 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 Dec 26, 2015 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. Dec 26, 2015 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. Dec 26, 2015 at 8:52

1 Answer 1


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; }
  • 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, 2016 at 8:50
  • @aaron Hope the elaborated answer makes more sense ..
    – TheMadsen
    Sep 11, 2016 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, 2019 at 13:42
  • @TheMadsen I assume I cannot use existing tmux sessions on the remote server, when attaching with tmux-attach? Also, if I use Zsh as my default shell, what to change in your script? [I managed to run tmux-ssh on the local machine, but I cannot even copy a single line in an existing tmux session on the remote machine. On the remote machine, tmux-attach does not take any input argumnent and I cannot create dedicated sessions.] I was using WSL locally, where originally was able to copy things to the local clipboard.
    – llinfeng
    Mar 16, 2021 at 4:36
  • 1
    @linfeng: Actually it will if you run the following from your shell: eval $(tmux show-environment -s -t 0) maybe even put in your $PROMPT_COMMAND (bash)
    – TheMadsen
    Mar 17, 2021 at 6:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .