How to send a command to all panes in tmux?

后端 未结 8 1152
夕颜
夕颜 2021-01-29 17:13

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.

8条回答
  •  既然无缘
    2021-01-29 18:03

    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.

提交回复
热议问题