我喜欢称它为:带有巨大卷轴的窗格上的清除历史。但是,我想通过脚本将此命令发送到各个窗口中的所有窗格。
由于这个问题,我知道如何向所有窗口发送命令,但如何向哪个窗口的所有窗格发送命令呢?
我想到了tmux manpage中的发送键和同步窗格,但我不确定如何将它们结合在一起。但也许有一种更简单的方法。
额外的观察:
稍微考虑一下,tmux list-panes -a似乎列出了当前会话中的所有窗格。开始的时候很有用。我该往哪里走?
我喜欢称它为:带有巨大卷轴的窗格上的清除历史。但是,我想通过脚本将此命令发送到各个窗口中的所有窗格。
由于这个问题,我知道如何向所有窗口发送命令,但如何向哪个窗口的所有窗格发送命令呢?
我想到了tmux manpage中的发送键和同步窗格,但我不确定如何将它们结合在一起。但也许有一种更简单的方法。
额外的观察:
稍微考虑一下,tmux list-panes -a似乎列出了当前会话中的所有窗格。开始的时候很有用。我该往哪里走?
当前回答
这是我的实用工具函数,只在窗格中没有任何运行时执行命令。
#!/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
}
以一个名为dev的会话为目标,在bash或zsh中的所有窗格中启用python virtualenv,避免在vim或任何其他可执行文件的窗格中执行命令:
_send_bash_command_to_session dev workon myvirtualenv
或者更容易记住:在当前会话中执行:
tmux_set_venv myvirtualenv
用这个函数找到我的配置文件。
其他回答
默认情况下,byobu使用tmux作为后端。它是一个让事情变得更简单的包装器:
Shift + F9:
Ctrl + F9:
Shift + F1
tmux send-keys -t <session id> <command> C-m
请相应地替换“session id”和“command”。
你可以在一个快捷方式中组合同步窗格和发送键,向所有窗格发送命令:
预定义tmux命令clear-history:
bind-key C set-option -w synchronize-panes on\; clear-history \; set-option -w synchronize-panes off
提示任意tmux命令:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; %% ; set-option -w -u synchronize-panes"
提示任意shell命令:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; send-keys %%\\n ; set-option -w -u synchronize-panes"
你试过在tmux窗口中跟随多个窗格吗
Ctrl-B :
setw synchronize-panes on
clear history
我的tmux版本是1.9a,这对我来说是可行的,一个键足够开和关
bind-key X set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"