Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Last active June 7, 2025 02:09
Show Gist options
  • Save samoshkin/05e65f7f1c9b55d3fc7690b59d678734 to your computer and use it in GitHub Desktop.
Save samoshkin/05e65f7f1c9b55d3fc7690b59d678734 to your computer and use it in GitHub Desktop.
tmux.conf excerpt to toggle on/off session keybindings and prefix handling
bind -T root F12 \
set prefix None \;\
set key-table off \;\
set status-style "fg=$color_status_text,bg=$color_window_off_status_bg" \;\
set window-status-current-format "#[fg=$color_window_off_status_bg,bg=$color_window_off_status_current_bg]$separator_powerline_right#[default] #I:#W# #[fg=$color_window_off_status_current_bg,bg=$color_window_off_status_bg]$separator_powerline_right#[default]" \;\
set window-status-current-style "fg=$color_dark,bold,bg=$color_window_off_status_current_bg" \;\
if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
refresh-client -S \;\
bind -T off F12 \
set -u prefix \;\
set -u key-table \;\
set -u status-style \;\
set -u window-status-current-style \;\
set -u window-status-current-format \;\
refresh-client -S
wg_is_keys_off="#[fg=$color_light,bg=$color_window_off_indicator]#([ $(tmux show-option -qv key-table) = 'off' ] && echo 'OFF')#[default]"
set -g status-right "$wg_is_keys_off #{sysstat_cpu} | #{sysstat_mem} | #{sysstat_loadavg} | $wg_user_host"
@niqodea
Copy link

niqodea commented Dec 9, 2023

Hey all, I just released tmux-nested, a plugin that takes inspiration from this gist to support arbitrary levels of nesting. Check it out, it might be of interest!

@afh
Copy link

afh commented Jun 8, 2024

Folks interested in a shorter wg_is_keys_off might be interested in the following:

wg_is_keys_off="#{?#{==:#(tmux show-option -qv key-table),off},#[reverse]OFF,}"

@cizra
Copy link

cizra commented Jul 31, 2024

I have 4-time nested tmux session (Because of my work, I have to ssh 3 time to move between networks).

Completely unrelated to tmux, but have you heard of the ProxyJump setting of OpenSSH, @Delt-A ?
For instance, if you have machines named Bastion, Firewall, and Gateway, and Devbox, you set it up ~/.ssh/config like this:

Host bastion
  Hostname bastion.mywork.com

Host firewall
  Hostname firewall.lan
  ProxyJump bastion

Host gateway
  Hostname gateway.inner
  ProxyJump bastion,firewall

Host devbox
  Hostname devbox.lan
  ProxyJump bastion,firewall,gateway

Now you should be able to connect any of these 4 hosts by name, just by typing ssh devbox or the like. I hope you have key-based authentication, so you won't have to type in your password 4 times. You can also override per-host port numbers or usernames, if needed.

If you need multiple sessions at the target machine, you could use tmux, or you could do SSH connection multiplexing. To enable, add this to ~/.ssh/config:

Host *
        ControlPath ~/.ssh/%C.sock
        ControlMaster auto
        ControlPersist 10m
        ServerAliveInterval 1
        ServerAliveCountMax 3

@qadzek
Copy link

qadzek commented Nov 12, 2024

This plugin might be helpful too, it allows you to suspend the local Tmux session: https://github.com/MunifTanjim/tmux-suspend

@etirta
Copy link

etirta commented Jun 7, 2025

This is a minimal configuration for OFF mode (tmux 3.0a):
...
* using a different keybind to enable and disable OFF mode means you can now technically go arbitrarily deep with the TMUX nesting

It may be arbitrary, but it's not unlimited and we have to remember various keys.

Here is the true unlimited nested T-Mux:

### Nested T-Mux
bind-key        -T prefix   C-\\ \
    set-option      prefix none \;\
    set-option      key-table off \;\
    set-option      status-position top \;\
    set-option      status-style reverse \;\
    if-shell -F '#{pane_in_mode}' 'send-keys -X cancel'
bind-key        -T off      C-\\ \
    set-option      key-table on \;\
    set-option      status-style reverse,blink
bind-key        -T on       C-\\ \
    send-keys C-\\ \;\
    set-option      key-table off \;\
    set-option      status-style reverse,noblink
bind-key        -T on       C-Space \
    set-option -u   prefix \;\
    set-option -u   key-table \;\
    set-option      status-position bottom \;\
    set-option -u   status-style

I used on key-table, but it's actually pre-on, but it's just too long.

To send the get-out key (Ctrl+) to child T-Mux, just repeat it, and so on. So once the child T-Mux also enter the on key-table, now you have to press Ctrl+\ 4x to send it to the grand-child T-Mux. It can be laborious, however it can be unlimited and the status indicator hopefully help us point out which T-Mux sessions are in the on key-table.

The only caveats if you actually have local T-Mux of the same users nested (by hacking it via TMUX= tmux) as in this case you will create a new session of T-Mux (connected to the same server). In this case, the set-option key-table ... from any T-Mux sessions connected to the same server will affect their siblings in such whoever in the prefix mode will reset back to root. This may be T-Mux bug or unintended consequences as the command should only effect the "current" session, i.e. no -g option is given.

If different users start the T-Mux, then it's fine as each user will have their own T-Mux server. If you must do locally nested T-Mux with the same user, then you need to force it to start a different server, via -L or -S option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment