I use vim inside tmux over an ssh session and I often run into the issue where the $DISPLAY variable gets changed, screwing up my copy-paste.

When I log in, $DISPLAY gets assigned to something new: localhost:B.0 let's say. When this happens, any open tmux pane still has the old DISPLAY saved localhost:A.0. Inside tmux that's easy enough to change, just export DISPLAY=localhost:B.0

But, any open VIM sessions will also have the old display, localhost:A.0. Now, I can quit out, change DISPLAY, and reopen vim, but that's a nuisance.

Is there a way to change OS environment variables from inside of vim?

I've tried:
- Suspending vim, ctr+z, setting the environment variable and bringing vim back to the foreground, fg
- Setting the vim DISPLAY via :let $DISPLAY='localhost:B.0'
- Running shell commands from vim :!export DISPLAY=localhost:B.0

But these don't work. Are there any other options?

  • Im a little confused: perhaps you can clarify your use/need. DISPLAY get set correctly for the shell/environment on the machine you login to, and you want that to back-propagate to your original machine? Why? In general, affecting parent environments from a subprocess is tricky and unnecessary.
    – D. Ben Knoble
    Dec 19, 2018 at 4:59
  • Here's some good explanations on the display variable and copy+paste stackoverflow.com/a/50608243/1322179, gist.github.com/antonkratz/…, vim.wikia.com/wiki/…
    – Ben
    Dec 19, 2018 at 5:06
  • 1
    :let $DISPLAY=... should work to set the Display environment variable to a new value. However, when done after Vim has started, Vim won't re-initialize the connection to the X-Server, so that practically won't help you. There is currently some discussion on how to fix a broken X connection in Vim: github.com/vim/vim/issues/3649 Please check the mentioned patch and report back. Dec 19, 2018 at 7:15


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.