有没有办法设置 tmux 窗格的名称?然后在脚本中按名称引用该窗格?

luc*_*iet 6 scripting session tmux

我想命名一个 tmux 窗格,以便稍后在脚本中我可以专门引用该窗格。我对 tmux 还很陌生。我有一个 .tmux 配置,并编写了一两个脚本来设置一个带有一些窗格的窗口,但我确信我并不真正知道它们是如何一起工作的。

大多数情况下,我的脚本会执行以下操作:

tmux spit-window -h
tmux select-pane -t 0
tmux send-keys "run some command" C-m
Run Code Online (Sandbox Code Playgroud)

...并对下一个窗格重复相同的操作..

但我想做一些类似的事情

tmux split-window -h
tmux select-pane -t 0
tmux name-pane "tail of X log"
tmux send-keys "run some command" C-m
Run Code Online (Sandbox Code Playgroud)

然后在完成该配置后在另一个脚本中:

tmux selected-named-pane "tail of X log"
tmux send-keys "exit"
Run Code Online (Sandbox Code Playgroud)

当然,我只是循环遍历我想要退出的窗格列表。

有没有办法做这样的事情?

小智 5

NAMES AND TITLES中的部分讨论man tmux了窗格标题。

以下是相关摘录:

窗格的标题通常由窗格内运行的程序设置,并且不会由 tmux 修改。

我可以建议使用窗格 ID 号,而不是使用窗格名称。“pane id”是当前 tmux 会话的唯一编号。它只是一个以“%”为前缀的数字,例如“%5”。

这是获取当前窗格的窗格 id 的方法:tmux display-message -p "#{pane_id}"

通过将此 id 保存在某处,您可以轻松地在某处引用它。这是示例代码:

tmux split-window -h
tmux select-pane -t 0

# save a pane id to a shell variable
current_pane_id=$(tmux display-message -p "#{pane_id}")

# now save the shell variable to tmux user option (user options are prefixed with @)
tmux set -g @some_variable_name "$current_pane_id"
Run Code Online (Sandbox Code Playgroud)

稍后,当您想从另一个脚本引用保存的窗格时:

# get saved pane id to a shell variable
pane_id="$(tmux show -g @some_variable_name)"

# use -t flag to specify the "target" where the keys are sent
tmux send-keys -t "$pane_id" "exit"
Run Code Online (Sandbox Code Playgroud)