How to update environment variable of existing Tmux session


I use Byobu (which is a wrapper on Tmux) to maintain shell sessions on the many servers I login to. I connect, disconnect and reconnect to the same Tmux sessions on these servers. When I reconnect back to an existing Tmux session, a new value is assigned to the DISPLAY environment variable. If I create a new Tmux session, by opening a new split or tab, then Tmux sets the new DISPLAY value correctly for that session. However, existing Tmux sessions have their old DISPLAY value which gives the familiar cannot open display error. I can find out the new DISPLAY value and set it manually in the existing session, but that is cumbersome.


I found a simple solution to this problem. Whenever I SSH to a server, I save the new DISPLAY assigned to this server in a .display.txt file in the home directory. For example, this can be done in .bashrc:

echo $DISPLAY > ~/.display.txt

To pick up the new DISPLAY value from this file in existing sessions, create a shell alias or function that you can call when you realize that the environment variable is wrong.

For example, for Fish shell, I added a ~/.config/fish/functions/ file with these lines:

function set_display
    set --export DISPLAY (cat ~/.display.txt)

When I need to update DISPLAY, I just call set_display at the shell! πŸ™‚

Tried with: Byobu 5.74, Tmux 1.8 and Fish 2.2.0


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.