163

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.

Kris
  • 19,188
  • 9
  • 91
  • 111
  • 6
    not a programming question. Please try http://superuser.com/. Good luck. – shellter Dec 15 '11 at 13:41
  • 1
    I'm happy to ask for the question to be moved, however for context I use vim inside tmux to code and of course often have to ssh in to other servers which is where this problem occurs. – Kris Dec 15 '11 at 23:31

3 Answers3

248

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

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • Additionally I had to comment out my existing `last-window` binding as it was called when doing `C-a C-a`, as such: `#bind-key C-a last-window`. Note my prefix is `a`, not the default `b`. – Kris Dec 16 '11 at 15:59
  • 1
    `bind-key -n C-\ send-prefix` does not work. My tmux is not recognising the \. When I use a letter, it works. – darksky Jan 24 '13 at 21:16
  • @Darksky: What version of *tmux* are you using? It works for me with 1.7, 1.6, and 1.5. You will definitely need to add an extra level of quoting if you are making the binding from a shell command (i.e. `tmux bind-key -n 'C-\' send-prefix`); if you are having problems getting it to work from `~/.tmux.conf` (or a Prefix `:` command line), then you might try similar quoting (i.e. `bind-key -n 'C-\' send-prefix`). – Chris Johnsen Jan 25 '13 at 02:29
70

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

dessalines
  • 6,352
  • 5
  • 42
  • 59
9

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.

Kris
  • 19,188
  • 9
  • 91
  • 111
qun
  • 711
  • 8
  • 7
  • if someone has changed the default binder key in the outer tmux, then has to use the same default binder key in the inner tmux, for the above snippet to work! – Paschalis Jan 26 '16 at 00:02
  • i am modifying the prefix of my local tmux (to Ctrl+] in my case) so all the "inner" tmux-es on the servers have default binding for prefix – actionless Jul 17 '17 at 19:31