Yubin Kim
Yubin Kim

Reputation: 143

Is there a way to have Vim reconnect to a different X server?

I am using Vim inside tmux over an ssh connection on a remote machine. I access the remote server from several locations including work and at home. This usually causes stale $DISPLAY variables and associated problems, most of which I have resolved except for one:

If I have a vim session already opened, is there a way to change which X server it is connected to without closing and re-opening it? I need to be able to do this so I can copy snippets of text from Vim using the X clipboard. I normally keep multiple vertical splits, linebreak and line numbers on, so doing this through tmux is usually a giant hassle and would prefer to do it with vim.

Upvotes: 4

Views: 578

Answers (4)

llinfeng
llinfeng

Reputation: 1395

I added the following line to ~/.tmux.conf on the server and reloaded the tmux sessions there. This one-liner does the trick of automatically attaching to the local X-server when I connect to the tmux-session from multiple local clients using SSH. Each local client has its own X-server (X410 in my case).

set-option -g update-environment " DISPLAY"

How it works? According this post, the line is supposed to refresh the $DISPLAY variable for all panes. Here is another related post.


Setup: all my local clients are Windows 10 machines, running WSL (Pengwin distro as WSL2). My X-server is X410 downloaded from the Windows Store. The server that hosts the Tmux+vim sessions is a Linux Mint machine. On the server, the Tmux version is 3.3a.

Per my workflow, this means a lot. Especially that I don't have to repeat the following procedures: 1. SSH from another local machine; 2. Finding that clipboard won't work; 3. Save the work, exit Vim; 4. Start a new pane in the current Tmux session, and 5. Start Vim again, trying to load back to where I was.

Upvotes: 0

Gearoid Murphy
Gearoid Murphy

Reputation: 12116

There's a relatively new command called xrestore which does this:

:xrestore :0

https://groups.google.com/g/vim_dev/c/G54XTJHAqXI

https://github.com/vim/vim/issues/3649

Upvotes: 3

Gavin Smith
Gavin Smith

Reputation: 3154

You can use xpra to display a running X11 program (specifically, vim) on different X11 servers.

This program functions for X11 programs like GNU screen or tmux do for terminal programs. xpra provides an X server for running programs and itself acts as an X client for the X server for the user's display.

You mention that you are running vim through tmux; you should maybe run vim outside of tmux and use xpra directly. As far as I know the program has to be run through xpra from the start.

Upvotes: 4

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

If the sole purpose of using X is to transfer large amount of text, consider using scp instead.

From within vim mark the lines you're interested in and do :w /tmp/foo

From a shell (or suspend vim using ctrl-z ; bg) do

scp [email protected]:/tmp/foo /some/local/directory

Upvotes: 2

Related Questions