42

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.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
Phluks
  • 906
  • 1
  • 9
  • 14
  • Thanks for the reminder George. – Phluks Apr 23 '13 at 08:12
  • 7
    There is an easier way for what I want to achieve. Using the '-L' (or '-S') option. These start a new server with the current environment. Ex. 'tmux -L newenv' Every encantation creates a new session in the server on that socket, with the same environment. (You can choose your own name of course) 'tmux -L newenv list-sessions, Lists the sessions on that particular server. ... Etc. – Phluks Apr 23 '13 at 08:18

1 Answers1

70

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.

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • 4
    Great response. One note: the ~/.tmux.conf file will not be read when the tmux server is already running, so changes like this will not take effect. I had expected it to be read during 'tmux attach-session', but it was not applied. – Wade May 18 '12 at 17:14
  • Sorry to resurrect this but the man page says "The update-environment session option may be used to update the session environment from the client when a new session is created or an old reattached". This suggest it should update them when reattaching to _existing_ sessions but it doesn't seem to. Any ideas? – sjbx Jan 29 '13 at 13:10
  • 8
    @CraftyThumber: If you are expecting the environments of existing shells to be updated, there is no way to do that from *tmux* — processes are independent once they have been forked. New children (e.g. windows/panes) will inherit the newly-updated session environment, but existing one can not be updated by *tmux*. – Chris Johnsen Jan 29 '13 at 20:23
  • 8
    Thanks @ChrisJohnsen, I realise that now. FWIW, I wrote a quick bash function to pull out the latest `tmux showenv` and update itself accordingly. Therefore on reattach I can update the shell's environment with a call to the function in my `.bashrc`: https://gist.github.com/4672606 – sjbx Jan 30 '13 at 11:26
  • This works going forward(inheriting), but if I split the tmux window, move to that new window, and type `deactivate` it removes the $VIRTUAL_ENV variable, modifies the $PS1 variable back to original, but doesn't re-modify the $PATH variable, as such `which python` still points to the virtualenv version of python – bk201 Jun 28 '16 at 05:02
  • @ChrisJohnsen I am working on a linux-gnu cluster and can't find ~/.tmux .conf, although I do have Tmux installed and I actively use it. What am I missing? – aerijman Jan 17 '20 at 14:49
  • How did you find this default? I was not able to find it in the man page. A grep of the source code shows: https://github.com/tmux/tmux/blob/3.1b/options-table.c#L541 – davidvandebunte Jun 28 '20 at 13:00