6

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.

3 Answers 3

9

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

tmux set-environment DISPLAY $DISPLAY
2

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.

The default is "DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY".

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.

2

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
shell_grep="bash|zsh"

# 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
2
  • 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. Sep 27, 2019 at 16:01
  • Thanks, yea they should really add this feature to tmux!
    – Mike Boiko
    Sep 28, 2019 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .