5

I have found a similar question here but without a working answer for me: System Clipboard Vim within TMUX within SSH session

I'm using Gnome terminal to start a ssh session with X forwarding to Debian 10. If I start neovim and copy (yank) text, then this text is copied to the Gnome clipboard and everything is fine.

This is the content of .ssh/config :

Host nuc
    ForwardX11 yes

I have this in .vimrc:

set clipboard^=unnamed,unnamedplus

But when I start neovim inside tmux, then this doesn't work.
I have tmux with the tmux-yank plugin and this works because when I copy from neovim inside tmux and then exit tmux I can see the selected text with xsel -o

How can I get the selection forwarded to my system clipboard?

1 Answer 1

4

Vim and NeoVim support for clipboard use a connection to the X11 server. The address to connect is available from the $DISPLAY environment variable.

The issue with X11 and terminal multiplexers or session managers such as tmux or screen is that the environment of the shells and programs running inside them will be the environment of when the tmux session was first created. That includes the $DISPLAY variable. So it means vim inside tmux will be trying to use the address of the X11 server of when the tmux session was created, not the one from where you just connected now.

A dirty but simple workaround is to update the $DISPLAY variable when you reconnect to tmux, to ensure you'll be connecting to the correct X11 server. Note that you need to do that for every shell or program running inside tmux, since each of them will have its own out-of-sync copy of the environment variable.

Something like the following works:

$ ssh -X nuc
nuc$ echo $DISPLAY
:1234.1
nuc$ tmux attach
tmux$ export DISPLAY=:1234.1
tmux$ vim

This should make clipboard work for that particular Vim.

As mentioned, if you have many tmux windows and panes, you'd have to update $DISPLAY on all of them. Also, if you create new windows or panes, they will start with the wrong $DISPLAY setting too (though you can also update the value of $DISPLAY in tmux's environment to fix new windows and panes, see tmux's set-environment command for that.)

5
  • @r03 Then Vim clipboard should be working... Is it not? Does it work on that SSH session outside tmux but not inside tmux?
    – filbranden
    Commented Apr 4, 2020 at 17:46
  • 1
    I was just gonna say that $DISPLAY is the same in ssh and inside tmux and this is not the solution, but I noticed that it just works now. This might be the solution
    – roeland
    Commented Apr 4, 2020 at 17:46
  • 1
    @r03 You might get "lucky" sometimes that a new connection will have a matching $DISPLAY... But on the other hand you might have them not matching, in which case Vim will not be able to access the clipboard...
    – filbranden
    Commented Apr 4, 2020 at 17:48
  • 1
    yes, an other window inside tmux had an empty $DISPLAY value and there it didn't work. After setting the correct value it worked perfect. Thanks!
    – roeland
    Commented Apr 4, 2020 at 17:50
  • 1
    I created the original tmux session without using X forwarding in ssh. After that I used X forwarding in ssh, but that means the existing windows already had the wrong DISPLAY value. I now created a new tmux session and it works perfect.
    – roeland
    Commented Apr 4, 2020 at 17:57

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