Send a command to every pane/window/session in tmux
— Kaushal ModiFaster way to send the same command to each and every pane in your tmux session.
- Optimize the
list-panes
+send-keys
tmux
commands, rewrite the post.
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 are few excerpts from my .tmux.conf
that allow doing just
that.
Send command to all panes in all sessions #
Thanks to the tip in comments from Bob Fleming, I learned that tmux
has a -a
switch for the list-panes
command.
From man tmux
,
list-panes [-as] [-F format] [-t target] (alias: lsp) If -a is given, target is ignored and all panes on the server are listed. If -s is given, target is a session (or the current session). If neither is given, target is a window (or the current window). For the meaning of the -F flag, see the FORMATS section.
With that knowledge, the older version of the E binding now reduces to,
# Send the same command to all panes/windows/sessions
bind E command-prompt -p "Command:" \
"run \"tmux list-panes -a -F '##{session_name}:##{window_index}.##{pane_index}' \
| xargs -I PANE tmux send-keys -t PANE '%1' Enter\""
Usage #
- Type the following binding in any
tmux
pane: C-z E1 - Enter a command that you would want to send to all the panes, like
source ~/.alias; clear
(this is entered in the tmux command prompt). - That will source the
~/.alias
in all panes, and then clear the terminals as well.
About the ##
#
The #
character needs to be escaped by another #
and typed as
##
, only when used inside the run-shell
command.
.. 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.
With the hashes escaped, those variables will be evaluated at run time.
But if you were to type the above command directly in the terminal,
without the run-shell
command wrapper, you would use only single
#
:
tmux list-panes -s -F "#{session_name}:#{window_index}.#{pane_index}"
Send command to all panes in current session #
The list-panes
command has another useful switch: -s
, which takes
an optional argument, a session name. If that argument is not
supplied, it takes the current session name by default.
Below C-e binding is used to send a command to all panes, in all windows, but only in the current session.
bind C-e command-prompt -p "Command:" \
"run \"tmux list-panes -s -F '##{session_name}:##{window_index}.##{pane_index}' \
| xargs -I PANE tmux send-keys -t PANE '%1' Enter\""
Older version (circa 2014) #
# 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\""
I have set my tmux prefix to C-z. ↩︎