在另一个 tmux 会话中打开 tmux 会话时如何发送命令? [英] How to send commands when opening a tmux session inside another tmux session?
问题描述
典型情况可能是:
$ tmux
[0] $ ssh example.com
$ tmux attach
[0] $
我打开一个 tmux 会话,然后通过 ssh 连接到服务器并附加到现有的 tmux 会话.在这一点上,我在另一个里面有一个 tmux 会话.如何向内部 tmux 会话发送命令?
I open a tmux session, then ssh in to a server and attach to an existing tmux session. At this point I have one tmux session inside another. How do I send commands to the inner tmux session?
注意:两个 tmux 会话具有相同的键绑定.
Note: Both tmux sessions have the same key bindings.
推荐答案
send-prefix
命令可用于将您的前缀击键发送到(在其中运行的进程)活动窗格.默认情况下,前缀是 Cb 并且 Cb 绑定到 send-prefix
(所以点击它两次会发送一个 Cb 到活动窗格).这正是我们需要访问内部 tmux 实例的绑定.
The send-prefix
command can be used to send your prefix keystroke to (the process running in) the active pane. By default, the prefix is C-b and C-b is bound to send-prefix
(so that hitting it twice sends a single C-b to the active pane). This is just what we need to access the bindings of the inner tmux instance.
第一个 C-b 被外部"tmux 实例捕获作为其前缀键.第二个被外部"tmux 实例捕获并触发其C-b 绑定(send-prefix
).这会将 C-b 发送到外部实例的活动窗格.在此窗格中运行的进程是(最终通过 ssh 实例)内部"tmux 实例.它捕获 C-b 作为其前缀键.现在,您的下一次击键将通过外部 tmux 实例并被内部实例捕获以触发绑定.
The first C-b is captured by the "outer" tmux instance as its prefix key. The second one is captured by the "outer" tmux instance and triggers its C-b binding (send-prefix
). This sends a C-b to the outer instance’s active pane. The process running in this pane is (ultimately, through an ssh instance) the "inner" tmux instance. It captures the C-b as its prefix key. Now your next keystroke will be passed through the outer tmux instance and captured by the inner one to trigger a binding.
要在 tmux 的二级实例中触发 c 绑定(new-window
),您需要输入 CbCb c.对于 tmux 的第三级实例,您可以输入 C-b C-b C-b C-b c.
To trigger the c binding (new-window
) in a second-level instance of tmux, you would type C-b C-b c. For a third-level instance of tmux you would type C-b C-b C-b C-b c.
如果您经常处理 tmux 的多层,这种每个级别的加倍可能会很烦人.如果您可以节省一些其他密钥,您可以进行无前缀绑定以使事情(可能)更容易输入:
This doubling for each level can be annoying if you are commonly dealing with multiple layers of tmux. If you can spare some other key, you could make a non-prefixed binding to make things (possibly) easier to type:
bind-key -n C-\ send-prefix
bind-key -n C-^ send-prefix \; send-prefix
在二级tmux中创建新窗口:C-\c
在三级tmux中创建新窗口:C-^ c(或C-\ C-\ c)
Create new window in second-level tmux: C-\ c
Create new window in third-level tmux: C-^ c (or C-\ C-\ c)
如果您想要(轻松)发送到较低级别的 tmux 实例的 tmux 命令数量有限,则可以改用 send-keys
创建一些特定的绑定(可能只是在你的顶级 tmux 实例中):
If you have a limited number of tmux commands that you want to (easily) send to the lower-level tmux instances, you might instead use send-keys
to create some specific bindings (possibly just in your top-level tmux instance):
bind-key C-c send-keys C-b c
bind-key C send-keys C-b C-b c
在二级tmux中创建新窗口:C-b C-c
在第三级 tmux 中创建新窗口:C-b C
Create new window in second-level tmux: C-b C-c
Create new window in third-level tmux: C-b C
这篇关于在另一个 tmux 会话中打开 tmux 会话时如何发送命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!