在另一个 tmux 会话中打开 tmux 会话时如何发送命令?

发布于 2024-12-21 12:00:02 字数 236 浏览 4 评论 0原文

典型的情况可能是:

$ tmux
  [0] $ ssh example.com
      $ tmux attach
        [0] $ 

我打开一个 tmux 会话,然后 ssh 到服务器并附加到现有的 tmux 会话。此时,我在另一个 tmux 会话中有一个 tmux 会话。如何向内部 tmux 会话发送命令?

注意:两个 tmux 会话具有相同的键绑定。

A typical situation may be:

$ tmux
  [0] $ ssh example.com
      $ tmux attach
        [0] $ 

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?

Note: Both tmux sessions have the same key bindings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-12-28 12:00:02

send-prefix 命令可用于将您的前缀击键发送到活动窗格(在其中运行的进程)。默认情况下,前缀为 Cb 并且 Cb 绑定到 send-prefix (因此点击两次会发送一个 Cb< /strong> 到活动窗格)。这正是我们访问内部 tmux 实例的绑定所需要的。

第一个 Cb 由“外部”tmux 实例捕获作为其前缀键。第二个由“外部”tmux 实例捕获并触发其 Cb 绑定 (send-prefix)。这会将 Cb 发送到外部实例的活动窗格。在此窗格中运行的进程(最终通过 ssh 实例)是“内部”tmux 实例。它捕获 Cb 作为其前缀键。现在,您的下一次击键将通过外部 tmux 实例传递,并由内部实例捕获以触发绑定。

要在 tmux 的二级实例中触发 c 绑定 (new-window),您可以输入 Cb Cb c< /强>。对于 tmux 的第三级实例,您可以输入 Cb Cb Cb Cb c

如果您通常处理多层tmux,那么每个级别的加倍可能会很烦人。如果您可以腾出一些其他键,您可以进行无前缀绑定以使输入(可能)更容易:

bind-key -n C-\ send-prefix
bind-key -n C-^ send-prefix \; send-prefix

在二级tmux中创建新窗口:C-\ c< /strong>
在第三级tmux中创建新窗口:C-^ c(或C-\ C-\ c


如果数量有限您想要(轻松)发送到较低级别的 tmux 实例的 tmux 命令,您可以使用 send-keys 创建一些特定的绑定(可能只是在你的顶层tmux实例):

bind-key C-c  send-keys C-b c
bind-key C    send-keys C-b C-b c

在二级tmux中创建新窗口:Cb Cc
在第三级tmux中创建新窗口:Cb C

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.

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.

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.

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

Create new window in second-level tmux: C-\ c
Create new window in third-level tmux: C-^ c (or C-\ C-\ c)


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

Create new window in second-level tmux: C-b C-c
Create new window in third-level tmux: C-b C

时光无声 2024-12-28 12:00:02

要进入内部,请按住 Control 键并按 B 两次。

To access the inner, hold control and hit B twice.

一城柳絮吹成雪 2024-12-28 12:00:02

编辑:

建议使用Cq作为绑定键,因为它是默认的控制键命令为

解除屏幕冻结并让屏幕继续显示

此处发生这种情况,@Paschalis 提供了解决方案:

如果碰巧两次不走运(以 Cq 为前缀的远程 tmux 会话):输入 Cltr-q,然后输入 :,然后在 tmux 中输入:发送密钥 Cq

下面就是答案:


为了简单起见,在您的 ~/.tmux.conf 中添加以下行

bind-key -n C-q send-prefix

然后您可以直接使用 Cq作为远程 tmux 的绑定密钥。

EDIT:

I do NOT recommend use C-q as a bind-key, as it is a default control-key command for

un-freezes the screen and lets screen display continue

A situation happens here, and @Paschalis provides a solution:

if it happens to be twice unlucky (a remote tmux session with C-q as prefix): Type Cltr-q, then :, and enter in tmux: send-keys C-q

Below it is the answer:


To make it simple, add the below line in your ~/.tmux.conf

bind-key -n C-q send-prefix

Then you can directly use C-q as bind-key for your remote tmux.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文