56

Is it possible to bind a tmux command to a key combination and use it directly without first pressing the prefix?

I find C-b + n too cumbersome to switch panes, so I was wondering whether I could bind C-1 for example, to switch to pane #1.

Or perhaps there may be a way to make the shortcut in the terminal emulator to send C-b + 1 when I press C-1?

Thanks!

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Ivan
  • 4,479
  • 5
  • 25
  • 26
  • 1
    I just use `set -g prefix C-a`. – u1686_grawity Jan 27 '11 at 20:31
  • Me too, I just put C-b on my question because that's the default. – Ivan Jan 27 '11 at 23:05
  • Some terminals support key bindings, which can translate "C-1" to a sequence of characters "C-b","1", e.g. [alacritty](https://github.com/alacritty/alacritty), [xterm](https://invisible-island.net/xterm/), [urxvt](http://software.schmorp.de/pkg/rxvt-unicode.html). – ebk Jun 25 '20 at 13:34

3 Answers3

57

For your example, use:

bind-key -n C-1 select-pane -t 1

The -n argument is used to bind-key, it means no prefix.

krehwell
  • 3
  • 2
Autoplectic
  • 1,248
  • 1
  • 11
  • 8
  • 1
    Thanks! That almost gets it done, except it doesn't recognize numbers, it says `unknown key: C-1`. If I use a letter it works. Do you know how I can specify a number key? – Ivan Feb 03 '11 at 12:36
  • 8
    @Ivan: The usual codes for Control keystrokes come from ASCII. ASCII does not define codes for C-1..C-9. The standard ASCII control characters are C-@, C-a..C-z, C-[, C-\, C-], C-^, C-_, and C-?. Most terminals just do not support distinct codes for most modified keystrokes (though many support some modifiers for arrow keys and functions keys). You are probably better off using `F1` in place of `C-1` (also note that the pane numbers start at 0, not 1: bind F1 to selecting pane 0, F2 to 1, etc.). – Chris Johnsen Feb 24 '11 at 05:22
  • I updated the answer to clarify that -n is an alias for -T root and explained a bit about it and sourced the docs. – Elijah Lynn Jun 08 '22 at 00:41
7

I have a quibble with the accepted answer here.

According to tmux(1), -n is an alias for -T root. So including -n doesn't actually mean that there is "no prefix" as much as it means the command will be bound to the root table, which is "not recommended".

tmux(1):

The root table is used for keys pressed without the prefix key: binding ‘c’ to new-window in the root table (not recommended) means plain ‘c’ will create a new window

What this seems to be saying is that:

bind -n c new-window

Will create a new window when c is pressed, and indeed, it does.

The dry humor in the author's phrase "not recommended" is the fact that "c" will of course be pressed many times in the average tmux session, given that "c" is the third-most frequent letter in English.

For another example, here is fast tmux window switching (using PageUp/PageDown keys):

unbind NPage
unbind PPage
bind -n PPage previous-window
bind -n NPage next-window
  • Thanks for finding this, I was searching the `man tmux` page and wasn't finding it. Also, I read that as "not recommended" to bind 'c' to new-window, not an "in general it isn't recommended" just specific to that example. – Elijah Lynn Jun 07 '22 at 23:26
  • I also updated the accepted answer to clarify what -n does. – Elijah Lynn Jun 08 '22 at 00:42
  • And lastly, thanks, I still can't get the ctrl + Tab working but can for non-ctrl keys, like N etc. But your suggestion to use pgup/pgdn is working and is kinda faster, me thinks. I'm gonna try it out for a while. – Elijah Lynn Jun 08 '22 at 00:44
2

I like to use Alt+, to move left a window and Alt+. to move right. The choice might not make sense to you at first glance, but those keys are also used for <>, I'm just too lazy to add shift in there as well.

# switch windows without prefix
bind -n M-, select-window -t -1
bind -n M-. select-window -t +1

Same goes for panes. I like to use Alt+[ and Alt+].

bind -n M-[ select-pane -t -1
bind -n M-] select-pane -t +1