2

Whenever I copy with "+y it only works for the instance of vim I am running and I would expect it to write to my system clipboard, that I can access both outside of vim and from other vim instances.

I've looked into previous posts on the forum and they don't solve my issue.

  1. When I run vim --version, the output contains +X11 and gtk-3.0

  2. I'm using vimx.

  3. set clipboard=unnamedplus,unnamed doesn't help.

  4. Here's my output from :reg:

Type Name Content
  1   c  ""   ^JType Name Content^J<d0>
  2   c  "0   ^JType Name Content^J  c  ""   ^JType Name Content^J<d0>^J <bc>Sy)V
  3   l  "1   set clipboard^=unnamed^J
  4   l  "2   " Use standard clipboard ^J
  5   l  "3   set clipboard=unnamedplus^J
  6   l  "4   unname^J
Armin
  • 23
  • 4
  • 1
    I’ve had better luck with "*y, but I’m on a mac and I’m not sure if that makes a difference. – D. Ben Knoble Feb 20 '20 at 18:18
  • Do you have X11 running? If you look at echo $DISPLAY (either outside Vim, on the shell, or inside Vim using the :echo command), do you see some output indicating where Vim should connect for the clipboard? – filbranden Feb 21 '20 at 01:19
  • Also, just to double check... Does vim --version output include +clipboard? That's the one that actually matters for "+ and "*... But if you have +X11 and gtk and are using vimx, I'd expect that to be the case... – filbranden Feb 21 '20 at 01:20
  • do you have some kind of an unusual setup like using wayland or editing remote files using ssh? – Christian Brabandt Feb 21 '20 at 07:01
  • 1
    @filbranden hmm, echo $DISPLAY gives me :0. Could you please explain what that means?

    do you see some output indicating where Vim should connect for the clipboard?

    Not sure what you mean by this.

    – Armin Feb 21 '20 at 10:00
  • @ChristianBrabandt not really, I'm using Tmux most of the time, but even without it the problem persists. – Armin Feb 21 '20 at 10:04
  • 1
    @filbranden OK, so when I quit Tmux, echo $DISPLAY prints :1 and the problem disapears. That's a starting point. – Armin Feb 21 '20 at 10:05
  • 1
    @filbranden, export DISPLAY=':1' fixed this. Is it a reasonable thing to do? – Armin Feb 21 '20 at 10:11
  • @Armin Wrote a more detailed answer. Hopefully that addresses the issue for you. Unfortunately, there don't seem to be much more elegant solutions to this problem... Anyways, hopefully the answer will be helpful to you. – filbranden Feb 22 '20 at 05:57

1 Answers1

3

On Linux/Unix, the Vim clipboard support communicates with the X11 system to synchronize the selection referred to by the "+ and "* registers.

For this feature to work, Vim needs to be able to communicate with the X11 server.

X11 is designed in a way that allows you to run multiple instances on the same host (multiple "displays") and applications use the $DISPLAY environment variable to determine which X11 display to use (in other words, which X11 server to connect to.)

In your case, you noticed you had DISPLAY=:0 inside tmux, where you're running Vim... While outside of tmux your terminal emulator reports DISPLAY=:1... What that means is that Vim inside tmux is trying to connect to the wrong X11 server.

Using export DISPLAY=:1 in your shell inside tmux fixes your problem, by having Vim inside tmux connect to the correct X11 server. (You might need to also reset this in other tmux windows/panes or in new shells that you open in tmux.)

This problem is most likely related to you having started tmux on one X11 session, then later reconnecting to it from another X11 session (perhaps you logged out from the GUI and logged in again, or a similar operation.) Since the selection of X11 display is controlled by an environment variable, once you start tmux in a specific display, it will by default stick to that display, which will also be inherited by shells and processes spawned in that tmux instance.

(Unfortunately, there doesn't seem to be a more general solution to this problem that doesn't involve updating the $DISPLAY variable once you reconnect, or once you notice the issue...)

filbranden
  • 28,785
  • 3
  • 26
  • 71