How to quickly switch to n.10+ windows in tmux?

23,858

Solution 1

There are a few easy ways to do it.

  • Use C-b ' to select the window index.

  • Use C-b w to get an interactive index to choose from (0-9a-z).

  • Add bindings to cycle through quickly in tmux.conf

    bind -r C-h select-window -t :-
    bind -r C-l select-window -t :+

The -r in the last one lets you repeat the key without having to repeat C-b. Typically the second one is the least number of keystrokes.

Solution 2

You can use switch-client -T<keytablename> to configure a sequence of keys to directly access high-number windows.

For example I use this configuration to jump to windows 10-19 with Ctrl-b + - + [0-9]:

bind - switch-client -Tabove9
bind -Tabove9 0 select-window -t:10
bind -Tabove9 1 select-window -t:11
bind -Tabove9 2 select-window -t:12
bind -Tabove9 3 select-window -t:13
bind -Tabove9 4 select-window -t:14
bind -Tabove9 5 select-window -t:15
bind -Tabove9 6 select-window -t:16
bind -Tabove9 7 select-window -t:17
bind -Tabove9 8 select-window -t:18
bind -Tabove9 9 select-window -t:19

Detailed explanation:

As explained in the tmux manpage, switch-client can be used to set the current key table to create key combinations:

switch-client [-ElnprZ] [-c target-client] [-t target-session] [-T key-table]

...

-T sets the client's key table; the next key from the client will be interpreted from key-table. This may be used to configure multiple prefix keys, or to bind commands to sequences of keys. For example, to make typing ‘abc’ run the list-keys command:

               bind-key -Ttable2 c list-keys
               bind-key -Ttable1 b switch-client -Ttable2
               bind-key -Troot   a switch-client -Ttable1

So in my example I configured the - key to switch to a custom table named above9 and then configured keys 0 to 9 in that table to switch to windows 10 to 19.

This is equivalent to GNU screen's bind -c <class> (that's where I come from)

Share:
23,858

Related videos on Youtube

glitch
Author by

glitch

Updated on September 18, 2022

Comments

  • glitch
    glitch over 1 year

    Tmux by default comes with the following keybinding to quickly switch between windows numbered 0 to 9:

    C-b + 0-9
    

    With a couple of dozen different project folders, a few ssh sessions and other goodies in my tmux, I seem to always spill over the 10 windows I can quickly access, and I would love to figure out a quick way to switch to windows numbered 10 or higher. Right now I have to manually type out something along these lines in the tmux prompt:

    swap-window -t 12
    

    Which is quite time-consuming.

    Has anyone figured out a solid shortcut that helps you quickly access windows 10 and higher? I know weechat solved this problem with the two shorcuts:

    Alt + 0-9
    Alt+j followed by \d{2}
    

    Can anything along those lines be accomplished with tmux?

  • glitch
    glitch almost 10 years
    The first two commands are exactly what I was looking for, thank you Jason!
  • ijoseph
    ijoseph over 3 years
    Ah. I've fatfingered C-b w a bunch accidentally and have been spooked. Now I finally know what it's for.