9

In ~/.tmux.conf, with

 bind-key v split-window "vim"

I can launch vim in a split window.

how I can use two keys in a tmux bind-key ?
Example:

 bind-key vG split-window "vim +$"

5 Answers 5

7

You cannot. tmux only allows single-key bindings (either alone, using bind-key -n, or following the prefix key).

However, you might try binding "v" to an invocation of command-prompt:

bind-key v command-prompt "tmux-vim.bash %%"

where tmux-vim.bash looks something like

if [ $1 = "G" ]; then
    tmux split-window "vim +$"
fi

Then, after typing v to get to the command prompt, you would just type "G" and press Enter. "G" would be passed as the argument to tmux-vim.bash, and that script would take care of executing the tmux command you (originally) wanted to associate with "vG".

2
8

Answer

There is only one right solution for it:

    # you can use "vim +$" as well, but I don't think that +$ prefix have any sense without the file path argument...
    bind -T multiKeyBindings G split-window "vim" 
    bind v switch-client -T multiKeyBindings

If you want the possibility to pass custom arguments, you should use this one instead:

    bind -T multiKeyBindings G command-prompt 'split-window "vim %%"'
    bind v switch-client -T multiKeyBindings

More examples:

    # Toggle maximizing of current pane by typing PREFIX mm 
    bind -T multiKeyBindings m resize-pane -Z
    bind m switch-client -T multiKeyBindings

    # or without PREFIX
    bind -T multiKeyBindings m resize-pane -Z
    bind -n m switch-client -T multiKeyBindings

    # rename current window by typing PREFIX mr
    bind -T multiKeyBindings r command-prompt 'rename-window %%'
    bind -n m switch-client -T multiKeyBindings

The notable thing

You should use a unique key tablet's name for each multi keybinding. Example:

    bind -T multiKeyBinding1 G split-window "vim"
    bind v switch-client -T multiKeyBinding1

    bind -T multiKeyBinding2 m resize-pane -Z
    bind -n m switch-client -T multiKeyBinding2
2
  • For those wondering what is going on: A new keytable called "multiKeyBindings" is created. prefix + v will make tmux change ("switch-client") to this keytable. The other command can only be triggered when G is pressed in this keytable.
    – Kaligule
    Sep 2, 2020 at 17:34
  • Might be a good idea to name them e.g. MultiKeyBinding_v, MultiKeyBinding_m, etc...
    – Jasha
    Mar 10, 2021 at 20:53
4

As @chepner said, you cannot do this directly. What you can do is bind v to create a binding for G that does what you want and then unbinds itself.

bind-key v bind-key -n G split-window "vim +$" \\; unbind -n G

There are a couple of important things to note with this approach:

  1. This will conflict with existing top-level bindings (in this case G); If you want to have something bound to G and something else bound to vG your unbinding step needs to restore the original binding.
  2. tmux will segfault if your .tmux.conf includes a bind-key statement that is too long. If this becomes a problem, you can work around it by putting your context switching in bash scripts and then bind a key to run those scripts.

For a more involved example, see this gist.

3

After 2015, yes you can do this.

They add a new argument -T key-table to switch-client command to support this feature. Here's the commit: https://github.com/tmux/tmux/commit/bded7437064c76dd6cf4e76e558d826859adcc79

Maybe they released this feature at version 2.1, but I did not saw any release notes mentioned this.

Here's the way to archive this goal:

task: do something like: bind-key vG split-window "vim +$" code:

bind-key -T prefix v switch-client -T prefix_v
bind-key -T prefix_v G split-window "vim +$"
3
  • Why the -n doesn't work on these bindings?
    – Niing
    Apr 13, 2021 at 3:51
  • I really need to know how, if you know please tag me.
    – Niing
    Apr 13, 2021 at 4:54
  • @Niing sorry, I don't understand what you want
    – pjincz
    Jun 24, 2021 at 13:05
0

Beyond the command-prompt approach suggested on the other answers, you can have a second set of mappings using a different prefix:

     prefix key
         Set the key accepted as a prefix key.

     prefix2 key
         Set a secondary key accepted as a prefix key.

Thus you could do the following:

set -g prefix C-v
bind-key G split-window "vim +$"

to map to c-v g instead of c-b vG. The downside is that it will shadow any default mapping, as it will also be accessible through c-b g.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.