我喜欢称它为:带有巨大卷轴的窗格上的清除历史。但是,我想通过脚本将此命令发送到各个窗口中的所有窗格。
由于这个问题,我知道如何向所有窗口发送命令,但如何向哪个窗口的所有窗格发送命令呢?
我想到了tmux manpage中的发送键和同步窗格,但我不确定如何将它们结合在一起。但也许有一种更简单的方法。
额外的观察:
稍微考虑一下,tmux list-panes -a似乎列出了当前会话中的所有窗格。开始的时候很有用。我该往哪里走?
我喜欢称它为:带有巨大卷轴的窗格上的清除历史。但是,我想通过脚本将此命令发送到各个窗口中的所有窗格。
由于这个问题,我知道如何向所有窗口发送命令,但如何向哪个窗口的所有窗格发送命令呢?
我想到了tmux manpage中的发送键和同步窗格,但我不确定如何将它们结合在一起。但也许有一种更简单的方法。
额外的观察:
稍微考虑一下,tmux list-panes -a似乎列出了当前会话中的所有窗格。开始的时候很有用。我该往哪里走?
当前回答
你试过在tmux窗口中跟随多个窗格吗
Ctrl-B :
setw synchronize-panes on
clear history
其他回答
有点晚了,但我不想只是为了发送一个命令而设置和取消设置同步窗格,所以我围绕tmux创建了一个包装器函数,并添加了一个名为send-keys-all-panes的自定义函数。
_tmux_send_keys_all_panes_ () {
for _pane in $(tmux list-panes -F '#P'); do
tmux send-keys -t ${_pane} "$@"
done
}
我还为tmux命令创建了一个包装器,以简化对该函数的调用(为了方便)。包装器和上面的代码都在这里。
这允许我运行tmux send-keys-all-panes <命令>或tmux skap <命令将<命令>发送到所有窗格。
注意,tmux是我的包装器函数tmux_pp的别名。
你试过在tmux窗口中跟随多个窗格吗
Ctrl-B :
setw synchronize-panes on
clear history
如果你想把你的命令发送到每个会话的每个窗口的每个窗格,把这个添加到你的.bashrc:
send_command_to_every_pane() {
for session in `tmux list-sessions -F '#S'`; do
for window in `tmux list-windows -t $session -F '#P' | sort`; do
for pane in `tmux list-panes -t $session:$window -F '#P' | sort`; do
tmux send-keys -t "$session:$window.$pane" "$*" C-m
done
done
done
}
然后你可以这样使用它:
send_command_to_every_pane source ~/.bash_profile
如果您想要这种行为,请将“$*”更改为“$@”,但根据我的经验,这正是您想要的。
上面的答案对我来说都不起作用(tmux v2.3),但是这个从bash命令行起作用:
for _pane in $(tmux list-panes -a -F '#{pane_id}'); do \
tmux clear-history -t ${_pane} ; done
一个更通用的脚本,对于tmux命令,而不是'clear-history',只会用一个参数替换该元素,例如。1美元。如果您打算编写一个脚本来处理一系列tmux命令,那么一定要小心,因为“-t ${_pane}”将需要应用于每个命令。
请注意,tmux list-panes的-a参数必须覆盖所有会话中的所有窗口中的所有窗格。否则,只有当前tmux窗口中的窗格会受到影响。如果您打开了多个tmux会话,并且只想将该命令应用于当前会话中的窗格,请将-a替换为-s(这都在tmux手册页中)。
我没有mod点来直接评论上面的每个答案,所以这里是为什么他们不适合我:
我在@shailesh-garg的回答中遇到的问题是,同步只影响窗格内发出的命令,而不影响使用Ctrl-B发出的tmux命令:这是窗格外的命令。
@kshenoy的回答有三个问题:
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; I couldn't figure out how to send a new-line character without literally breaking the line; Even when I did that, sending "tmux clear-history" had no effect.
无可否认,这只是半相关的,我发现当我切换同步窗格时,我可以让状态背景变成红色,所以当我切换回一个同步窗格状态未知的窗口时,很明显:
bind-key C-x setw synchronize-panes on \; set-window-option status-bg red \; display-message "pane sync on"
bind-key M-x setw synchronize-panes off \; set-window-option status-bg default \; display-message "pane sync off"