Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a command to all panes in tmux?

Tags:

tmux

People also ask

How do I send a command to tmux session?

tmux may be controlled from an attached client by using a key combination of a prefix key, 'C-b' (Ctrl-b) by default, followed by a command key. The default command key bindings are: C-b Send the prefix key (C-b) through to the application. C-o Rotate the panes in the current window forwards.

What is Ctrl B in tmux?

ctrl-b, <arrow key> switch to the pane in whichever direction you press. ctrl-b, d. detach from tmux, leaving everything running in the background.

How do I switch between tmux panes?

Tmux uses the keybinding 'Prefix' followed by 'Ctrl+o' to cycle around the panes. When you use this key-binding for the first time, it moves the pane in one position clockwise.


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}"

None of the above answers worked for me (tmux v2.3), but this did, 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' would just replace that element with a parameter, eg. $1. Do be careful if you intend to write a script to handle a series of tmux commands, as "-t ${_pane}" will need to be applied to each.

Note that the -a parameter to tmux list-panes is required to cover all panes in all windows in all sessions. Without that, only panes in your current tmux window will be affected. If you have more than one tmux session open and only want to apply the command to panes within the current session, replace -a with -s (It's all in the tmux man page).

I haven't the mod points to comment directly on each of the above answers, so here's why they weren't working for me:

The problem that I had with @shailesh-garg 's answer was that the sync affected only commands issued within the panes, not tmux commands issued using Ctrl-B : which are outside the panes.

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

  1. it sends keystrokes to within a pane, not to the tmux operation of that pane, so for instance, if one had a bash shell running in the pane and one used the script to send "clear-history", those would be the keystrokes that would appear in the bash command-line. A work-around would be to send "tmux clear-history" or to pre-pend "tmux " to "$@", but I haven't edited the answer because of my other problems with the answer;
  2. I couldn't figure out how to send a new-line character without literally breaking the line;
  3. Even when I did that, sending "tmux clear-history" had no effect.

tmux send-keys -t <session id> <command> C-m  

Replace the "session id" and "command" accordingly.


This is my utility function to do it, only executing the command when there there is nothing running in the pane.

#!/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
}

Example targeting a session called dev, enabling a python virtualenv in all panes that are in bash or zsh, avoiding executing the command in panes with vim or any other executable:

_send_bash_command_to_session dev workon myvirtualenv

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

tmux_set_venv myvirtualenv

Find my configuration file with this function.