How do I start tmux with my current environment?

Phluks picture Phluks · Dec 27, 2011 · Viewed 20.9k times · Source

Or at least the part of it, that makes sense.

More specifically I have some environment variables, that have been exported by running a script, to create an adequate environment for the task at hand. When I run tmux these variables are nowhere to be seen, neither in the global or the session environment. Of course I can run this script again but ...

I'd be satisfied if I could specify the particular vars in my .tmux.conf file however:

set-environment VAR $VAR

Does not do what I'd expect.

Thanks in advance :)

Ah, I think I know why.

When starting a second session of tmux, say in another terminal, it copies the environment from the first one. The first one pretty much takes the current environment of the calling shell and adds some tmuxiness to it.

My current workaround is just stopping and starting my tmux sessions when i need to change environment.

Answer

Chris Johnsen picture Chris Johnsen · Mar 23, 2012

You should configure the tmux session option update-environment to include the variables you want to be updated when creating new sessions. The default value includes several common X11 and SSH variables:

DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY

To add your variables, use the set-option tmux command with its -g and -a flags (append to the existing “global” (default) value). In your ~/.tmux.conf:

set-option -ga update-environment ' YOUR_VAR'

Be sure to include the leading space so that your variable name is separated from the trailing name in the default value.