How to send a command to all panes in tmux?
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.
I know how to send a command to all the windows, courtesy of this question, but how do I send a command to all the panes of which window as well?
send-keys
and synchronize-panes
from the tmux manpage come to mind, but I'm not sure how to marry them together. But maybe there is a simpler way to do this.
Extra Observations:
Thinking about this a little bit, tmux list-panes -a
seems to list all the panes in the current session. Pretty useful to start off with. Where do I go from here?
您是否尝试过在多窗格的tmux窗口中执行下列操作
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.
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:
上一篇: 如何禁用Tmux中的键绑定
下一篇: 如何发送命令到tmux中的所有窗格?