A Scripter's Notes

Emacs, scripting and anything text oriented.

Send a command to every pane/window/session in tmux

Ever wondered how you would send the clear command to each pane in each window in each session in tmux, or how you would do source your shell config file in each after each tweak.

Here is an excerpt from my .tmux.conf file that allows sending a command to all my “terminals”, or to all panes in all windows in all sessions.

# Send the same command to all panes/windows/sessions
bind E command-prompt -p "Command:" \
       "run \"tmux list-sessions                  -F '##{session_name}'        | xargs -I SESS \
              tmux list-windows  -t SESS          -F 'SESS:##{window_index}'   | xargs -I SESS_WIN \
              tmux list-panes    -t SESS_WIN      -F 'SESS_WIN.##{pane_index}' | xargs -I SESS_WIN_PANE \
              tmux send-keys     -t SESS_WIN_PANE '%1' Enter\""

Usage

  • Type the following binding in any tmux pane: C-z E1
  • source ~/.alias; clear (this is entered in the tmux command prompt)
  • This will source the ~/.alias in allpanes and then clear the terminals as well.

Note that the tmux vars had to be escaped – double hashes ## were used instead of single hash # – because otherwise tmux run-shell command will replace the unescaped #{session_name}, #{window_index} and #{pane_index} with their current values before executing the command.

Now with the hashes escaped, the above variables will be evaluated at run time.


  1. I have set my tmux prefix to C-z. [return]

Comments

comments powered by Disqus