How to send a command to all panes in tmux?

I like to call :clear-history on panes with a huge scrollback. However, I want to script a way to send this command to all the panes in the various windows.

I know how to send a command to all the windows, courtesy of this question, but how do I send a command to all the panes of which window as well?

send-keys and synchronize-panes from the tmux manpage come to mind, but I'm not sure how to marry them together. But maybe there is a simpler way to do this.

Extra Observations:

Thinking about this a little bit, tmux list-panes -a seems to list all the panes in the current session. Pretty useful to start off with. Where do I go from here?


Have you tried following in tmux window with multiple panes

Ctrl-B :

setw synchronize-panes on

clear history

A bit late to the party but I didn't want to set and unset synchronize-panes just to send one command so I created a wrapper function around tmux and added a custom function called send-keys-all-panes.

_tmux_send_keys_all_panes_ () {
  for _pane in $(tmux list-panes -F '#P'); do
    tmux send-keys -t ${_pane} "$@"
  done
}

I also create a wrapper around the tmux command to simplify calling this function (for convenience). The wrapper and the above code are all here.

This allows me to run tmux send-keys-all-panes <command> or tmux skap <command to send <command> to all panes.

Note that tmux is aliased to my wrapper function tmux_pp.


Update June 2019

Quick illustration on how to configure your own binding for synchronize panes.

Added the following into my tmux.conf (the comments certainly apply to my overall configuration):

# synchronize all panes in a window
# don't use control S, too easily confused
# with navigation key sequences in tmux (show sessions)
unbind C-S
bind C-Y set-window-option synchronize-panes

Now, I can toggle the ability to synchronize commands across multiple panes with <C-a><C-y>.

(Yes, I remapped the bind key to Ctrl a).


my tmux version is 1.9a, and this works for me, one key is enough for both on and off

bind-key X set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"