I'm looking to switch from screen to tmux for my terminal multiplexing needs, but there's a feature of screen that I need to replicate in tmux: the -X option, which allows one to run arbitrary screen commands inside an existing screen session. This allows me to modify a running screen session's environment to point to a new X server, since my screen sessions tend to live longer than my X11 logins. For example, I could put something screen -X setenv DISPLAY $DISPLAY in my X11 startup scripts.

Anyway, is there a way in tmux to send arbitrary commands to a running tmux session? Alternatively, a way to update environment variables in a running tmux would be acceptable.


It's just tmux, optionally with the -t option to select a session (corresponding to -S for Screen).

tmux set-environment DISPLAY $DISPLAY
| improve this answer | |

Answering the part of the question about updating the environment: tmux by default inherits certain variables from the client's environment when creating or reattaching a session.

From the manpage:

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.


So you don't have to do anything to get the updated DISPLAY setting when reattaching a session.

Note that this only takes effect for new windows or panes you create in the old session, but not existing windows.

| improve this answer | |

If you're looking to update $DISPLAY automatically for each of your shell processes in your tmux session, you can check out my solution: https://www.reddit.com/r/tmux/comments/cd3jqw/automatically_update_display_for_each_tmux_pane/

To summarize:

Add line to tmux.conf set-hook -g client-attached 'run-shell /bin/update_display.sh'

Create script /bin/update_display.sh:

# tmux will only send-keys to the following active processes

# Update $DISPLAY for each tmux pane that is currently running one of the $shell_grep processes
tmux list-panes -s -F "#{session_name}:#{window_index}.#{pane_index} #{pane_current_command}" | \
    grep -E $shell_grep| \
    cut -f 1 -d " " | \
    xargs -I PANE tmux send-keys -t PANE 'eval $(tmux showenv -s DISPLAY)' Enter
| improve this answer | |
  • 1
    This should be higher. Best solution to update DISPLAY on existing windows (not only new ones) when re-attaching to old session. The fact that this isn't built into tmux is mind boggling. Such a common use case. – asyncopation Sep 27 '19 at 16:01
  • Thanks, yea they should really add this feature to tmux! – MikeBoiko Sep 28 '19 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.