Search code examples
tmuxkeymapping

Bindings with key sequences


Does Tmux supports key-bindings with key sequences like Vim does (e.g. bind-key ab kill-pane)? Or how can i emulate that?


Solution

  • Tmux supports only single character key bindings (unfortunately).

    So, only this:

    bind-key a kill-pane
    

    or this:

    bind-key b kill-pane
    

    Please note this is different from for example C-a (Ctrl-a) or M-a (Alt-a).

    Even though we users write those with multiple characters and even have to press 2 keys to invoke them, both Ctrl-a and Alt-a are actually a single character for tmux (and in general to my knowledge).

    Alternative

    ...might not be what you expect, but here it is:

    # in .tmux.conf
    bind a command-prompt -p "pressed a" "run '~/my_script %%'"
    

    And the example my_script file:

    #!/bin/bash
    
    case "$1" in
      b)
        tmux kill-pane
        ;;
      c)
        tmux kill-window
        ;;
    esac
    

    Now after you reload your tmux.conf and press prefix + a you'll get a tmux prompt saying 'pressed a'.

    Go ahead and press b and Enter. tmux kill-pane from the script will execute.

    Similarly if you press prefix + a + c and Enter you'll execute another option from the script.

    This kind-of mimics what you want with the addition of Enter key at the end.

    Also, the provided script is extendable so you can add more "bindings" to get prefix + a + d + Enter etc..