How to send a command to all panels in tmux?

I like to call :clear-history on huge scrollbars. However, I want a script way to send this command to all panels in different windows.

I know how to send a command to all windows kindly provided by this question , but how do I send a command to all window panes as well?

send-keys and synchronize-panes from the tmux man page come to mind, but I'm not sure how to marry them. But there may be an easier way to do this.

Additional observations:

Thinking about this a bit, tmux list-panes -a seems to list all the panels in the current session. It’s quite useful to start with. Where am I going from here?

+157
tmux
May 01 '13 at 19:56
source share
6 answers

Have you tried following in tmux window with multiple panels

 Ctrl-B : setw synchronize-panes on clear history 
+285
May 16 '14 at 21:01
source share

A little late for the party, but I did not want to install and remove the synchronization panels just to send one command, so I created a wrapper function for 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 shell for the tmux command to simplify the call to this function (for convenience). The shell 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 panels.

Note that tmux is associated with my tmux_pp wrapper function.

+24
Dec 03 '15 at 18:47
source share

None of the above answers worked for me (tmux v2.3), but this came from the bash command line:

 for _pane in $(tmux list-panes -a -F '#{pane_id}'); do \ tmux clear-history -t ${_pane} ; done 

A more generalized script, for tmux commands other than 'clear-history', will simply replace this element with a parameter, for example. $ 1. Be careful if you intend to write a script to handle a series of tmux commands, since "-t $ {_ pane}" will need to be applied to everyone.

Note that the -a option for tmux list-panes is required to cover all panels in all windows in all sessions. Without this, only the panels in the current tmux window will be affected. If you have multiple tmux sessions open and want to apply the command to the panels in the current session, replace -a with -s (all this on the tmux man page).

I do not have mods to comment directly on each of the above answers, so this is why they did not work for me:

The problem I ran into @ shailesh-garg's answer was that the synchronization affected only the commands issued in the panels, not the tmux commands issued with Ctrl-B : which are outside the panels.

The three problems I had with @kshenoy's answer were as follows:

  • it sends keystrokes to the panel, and not to the tmux operation of that panel, for example, if you had a bash shell working in the panel and one used the script to send "clear-history", there will be keystrokes that appear on the bash command line. A workaround would be to send "tmux clear-history" or tentatively defer "tmux" to "$ @", but I did not edit the answer due to my other answer problem;
  • I could not understand how to send a new-line character without literally breaking the line;
  • Even when I did, sending "tmux clear-history" had no effect.
+4
Aug 04 '17 at 21:02 on
source share
 tmux send-keys -t <session id> <command> Cm 

Replace “session identifier” and “command” respectively.

+2
03 Feb '18 at 21:12
source share

Update June 2019

A brief illustration of how to set up your own snap to synchronize panels .

The following has been added to my tmux.conf (comments certainly apply to my general configuration):

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

Now I can switch the ability to synchronize commands on multiple panels using <Ca><Cy> .

(Yes, I reassigned the anchor key to Ctrl a).

+2
Jun 03 '19 at 1:11
source share

This is my utility function, which does this only by executing a command when nothing is working on the panel.

 #!/bin/bash _send_bash_command_to_session() { if [[ $# -eq 0 || "$1" = "--help" ]] ; then echo 'Usage: _send_bash_command_to_session $session_name what ever command you want: ' return fi input_session="$1" input_command="${@:2}" for _pane in $(tmux list-panes -s -t ${input_session} -F '#{window_index}.#{pane_index}'); do # only apply the command in bash or zsh panes. _current_command=$(tmux display-message -p -t ${input_session}:${_pane} '#{pane_current_command}') if [ ${_current_command} = zsh ] || [ ${_current_command} = bash ] ; then tmux send-keys -t ${_pane} "${input_command}" Enter fi done } tmux_set_venv() { _current_session=$(tmux display-message -p '#{session_name}') _send_bash_command_to_session ${_current_session} workon $1 } 

An example aimed at a session called dev that includes python virtualenv on all panels in bash or zsh , avoiding running the command in panels with vim or any other executable:

 _send_bash_command_to_session dev workon myvirtualenv 

or easier to remember: to do this in the current session:

 tmux_set_venv myvirtualenv 

Find my configuration file with this feature.

0
Jun 26 '18 at 19:12
source share



All Articles