Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Last active February 2, 2025 07:21
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"
@jasonbeach
Copy link

As an FYI, if you're on Ubuntu 16.04 (possibly only the mate desktop?) a terminal emulator called tilda uses the F12 key and intercepts it before tmux can.

@aberezin
Copy link

I get a "bad style bg= fg=" message when I hit F12 using this. Im a tmux newbie.

@NathanRV
Copy link

I get a "bad style bg= fg=" message when I hit F12 using this. Im a tmux newbie.

You have to define the variables used with something like this :
#Variables
color_status_text="colour245"
color_window_off_status_bg="colour238"
color_light="white" #colour015
color_dark="colour232" # black= colour232
color_window_off_status_current_bg="colour254"

@bs
Copy link

bs commented May 28, 2020

This is amazingly useful. Thank you!

@ajvirgona
Copy link

Thank you!
I've been using tmux locally and remotely for a long time but avoiding nesting by opening a special tmuxless terminal window for my remote sessions. All the while wishing there was an elegant solution to "tmuxing while I tmux". This solution is great! such a simple idea. Looking forward to trying it out.

@llraphael
Copy link

This is the one I have been looking for. Thanks for sharing!

@frenchy64
Copy link

To display the user/host in the status bar, needs the following from the original config:

wg_user_host="#[fg=$color_secondary]#(whoami)#[default]@#H"<br class="Apple-interchange-newline">`

@Delt-A
Copy link

Delt-A commented Oct 7, 2022

image
I have 4-time nested tmux session (Because of my work, I have to ssh 3 time to move between networks). So do you have any idea to switch between the first and the last tmux? Or better if we can easily control each of them. Sorry for my bad English.

@Jakob-Koschel
Copy link

@Delt-A do you need to have a tmux session on every jump to your target network? couldn't you use something like https://www.redhat.com/sysadmin/ssh-proxy-bastion-proxyjump to ssh directly into the target (using the others only as jumps) and then have one tmux session there?

@fidodido48
Copy link

Hi,
I've got 2 sessions - (outer) s1 and inside there is (inner) s2.
When I press F10 (got toggle key bound to F10) on the keyboard it works as expected, but trying to do the same in the script ('tmux send -t s1 F10') disables not the outer, but the innser session (s2 instead of s1).

Here's terminal output:

$ tmux send -t s1 F10 (disables s2)
$ tmux send -t s2 F10 (echoes below)
^[[21~%)

Any ideas how to make it work?

@niqodea
Copy link

niqodea commented Jul 2, 2023

This is a minimal configuration for OFF mode (tmux 3.0a):

# Activate OFF mode
bind -n M-o \
    set prefix None \;\
    set key-table off \;\
    set status-style "fg=colour245,bg=colour238"

# Disable OFF mode
bind -T off M-O \
    set -u prefix \;\
    set -u key-table \;\
    set -u status-style
  • setting the status-style is enough for my liking
  • removing the client refresh seems to still work just fine for some reason
  • using a different keybind to enable and disable OFF mode means you can now technically go arbitrarily deep with the TMUX nesting

@phuntik
Copy link

phuntik commented Sep 30, 2023

@niqodea

This is a minimal configuration for OFF mode (tmux 3.0a):

wow man, this minimal setup is so good. appreciate

@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

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