Display env variable, tmux and zsh over ssh
I have been using zsh within tmux, and found upon reattaching tmux X forwarding wasn’t working. For example when trying to launch gvim I’d get the error:
E233: cannot open display
The problem, a quick google determined, is that each time I ssh into my sever a new
$DISPLAY environment variable is set. When I run ’tmux attach’ the new $DISPLAY variable is passed through (see http://stackoverflow.com/questions/8645053/how-do-i-start-tmux-with-my-current-environment) so any new windows within tmux will have the correct environment. However the environment of any existing windows can’t be changed, causing the problem.
The best solution I found was proposed by Alex Teichman here: http://alexteichman.com/octo/blog/2014/01/01/x11-forwarding-and-terminal-multiplexers/ However I had two problems:
- It doesn’t seem to work with zsh rather than bash. I guess this is due to the behaviour of preexec() being different, but I couldn’t quickly work this out from the zsh manual.
- It maybe felt slightly inelegant to update $DISPLAY every single time a command is run
My solution is pretty similar. I add the following to ~/.zshrc:
echo $DISPLAY > ~/.display.txt alias up_disp='export DISPLAY=\`cat ~/.display.txt\`'
This writes the correct $DISPLAY variable to a hidden file when a session is started (i.e. when I connect to the server). When I find forwarding isn’t working, I just run up_disp in that window. Not the perfect solution, but it works ok for me