Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to update environment variable of existing Tmux session

📅 2016-May-06 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ byobu, display, fish, tmux ⬩ 📚 Archive

Problem

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.

Solution

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/set_display.fish file with these lines:

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

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


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧