1
votes

I'm trying to setup tmux to work like so:

  • Outer session (level 0) on localhost with prefix C-a
  • Inner session (level 1) on localhost with prefix C-b
  • Inner session (level 1) on remote host with prefix C-b

So each inner session is nested directly in the outer session.

If I setup my .tmux.conf like this:

unbind C-b
set -g prefix C-a
bind-key -n C-b send-prefix

the local inner session receives the C-b prefix properly, but remote inner session doesn't.

If I change my tmux.conf to this:

unbind C-b
set -g prefix C-a

then the remote inner session receives C-b, but the local session doesn't.

Is there a way I can configure tmux so that both the local and remote nested sessions receive the C-b prefix?

2

2 Answers

1
votes

Inspired by this blog post on Tmux scripting, I was able to find a solution. I put this in my shell startup script (e.g. .bashrc) on my localhost:

tmux_outer() {
  SESSION=$1
  tmux -2 new-session -d -s $SESSION
  tmux set prefix C-a
  tmux -2 attach-session -t $SESSION
}

tmux_inner() {
  SESSION=$1
  env TMUX='' tmux -2 new-session -s $SESSION
}

and removed my .tmux.conf

The problem in my original .tmux.conf:

unbind C-b
set -g prefix C-a
bind-key -n C-b send-prefix

seems to be set -g prefix C-a, which globally sets the prefix to C-a. In my shell function, I create the outer session in a detached state, set the prefix to C-a only for that session, and then attach to that session.

When I start an inner session on my localhost or remote host, it has the default tmux prefix C-b. Since the outer session isn't trying to capture and send C-b, both inner sessions receive C-b with no problem.

0
votes

tmux manual says, if I read it correctly, 1) prefix is server-level setting and 2) all sessions are managed by a single server. This means you can't get outer and inner session at the same host with different prefixes, unless you are starting multiple servers using corresponding options as non-default socket and another config for a second server.

Alternatively, it's easier to use GNU screen instead of tmux. It allows prefix specification in command line and unlimited server count in a easier way.

UPDATE[2017-07-05]: modern tmux has enough per-session configuration, including switch prefix, despite in somewhat cumbersome way.