views:

829

answers:

3

I use screen to persist my work session and connect to the same session from multiple machines. How can I setup SSH and screen such that the XDISPLAY variable inside my persistent screen session is always set to the machine I am currently connecting from?

ie. I start the screen session at work and use gvim, which uses the X server running on my work machine. Later, I connect to the same session from home and also want to use gvim. But this time, I want gvim to use the X server on my home machine. I realize I could manually update XDISPLAY every time I connect from a different machine but I'd rather have an automated system.

Bonus points if I can actually move gvim from my work machine to my home machine while it is running. I tried xmove but could never get it to play nice.

A: 

I'm also interested in the answer for this. Shouldn't it be possible to list the SSH processes for your user sorted by time and cat the environment for that process maybe?

Hugo
+3  A: 

There is no "trivial" way to change environment variables in foreign processes.

A straightforward solution might be to persist your XDISPLAY into a file on login and use a PROMPT_COMMAND to read this file before printing the next prompt.


For moving X applications around look at something like X11vnc or Xvnc.

David Schmitt
+3  A: 

The following is a manual solution, but there's no reason you couldn't use an alias or a script to have it done automagically when you remotely log in.

Assuming that your local shell sets the DISPLAY variable appropriately, you could use screen -X to send the following commads to your remote screen before connecting.

# set future remote shells started by screen to have the correct XDISPLAY
% screen -X "setenv XDISPLAY $DISPLAY" #...

# set up the keystroke F1 to update the XDISPLAY in current shells
% screen -X "bindkey -k k1 stuff export XDISPLAY=$DISPLAY\015" #...

If you know that all your windows were left in a shell (not a running editor or some such), you could use :at to change the XDISPLAY rather than a key binding:

# update the XDISPLAY in all current windows
% screen -X "at % stuff export XDISPLAY=$DISPLAY\015" #...

Alternately, if you know some way of grabbing a parent process's environment variable value, then you could use that together with your shell's prompt hook to grab SCREEN's value of XDISPLAY (as set by setenv) and update it for the shell.

rampion