folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
4.87k stars 161 forks source link

Collapse multiple keybindings under a single label? #236

Closed steve-lorimer closed 5 days ago

steve-lorimer commented 2 years ago

Is it possible to configure multiple keybindings under a single which-key label?

For example, I have the following similar bindings:

If I just add all the above to which-key I end up with the popup completely swamped with a multitude of similar key mappings

1 -> Select window 1        4 -> Select window 4        7 -> Select window 7
2 -> Select window 2        5 -> Select window 5        8 -> Select window 8
3 -> Select window 3        6 -> Select window 6        9 -> Select window 9

I would instead like to "collapse" them all into a single menu item

1..9 -> Select window 1..9

I would like to do something like:

require("which-key").register({ [ "<leader>1..9" ] = { "Select window 1..9" } })

However, this seems to fail somewhere - the keybinding isn't added to which-key.

Is there a way to have which-key display something like 1..9 -> Select window 1..9?

steve-lorimer commented 2 years ago

What I'm hoping for is to replicate the 1..9 in the below example image, which is taken from the spacemacs config in emacs

spacemacs

EtiamNullam commented 2 years ago

You can hide some entries using which_key_ignore key, for example:

...
['2'] = { '<cmd>echo 2<CR>', 'which_key_ignore'},
...

Still I don't know how to display something that looks like 1..9 or so.

You can also get some different approach, move 1..9 to a subgroup to reduce clutter:

...
w = {
    name = 'Go to window',

    x = '1..10', -- group has to contain some visible entry

    ['1'] = { '<cmd>echo 1<CR>', 'which_key_ignore'},
    ['2'] = { '<cmd>echo 2<CR>', 'which_key_ignore'},
    ['3'] = { '<cmd>echo 3<CR>', 'which_key_ignore'},
    ['4'] = { '<cmd>echo 4<CR>', 'which_key_ignore'},
    ['5'] = { '<cmd>echo 5<CR>', 'which_key_ignore'},
    ['6'] = { '<cmd>echo 6<CR>', 'which_key_ignore'},
    ['7'] = { '<cmd>echo 7<CR>', 'which_key_ignore'},
    ['8'] = { '<cmd>echo 8<CR>', 'which_key_ignore'},
    ['9'] = { '<cmd>echo 9<CR>', 'which_key_ignore'},
    ['10'] = { '<cmd>echo 10<CR>', 'which_key_ignore'},
},
...

Or jump one by one (repeatable):

...
w = {
    name = 'Window',

    j = { '<C-w>j<cmd>WhichKey <LT>Leader>w<CR>', 'Go down' },
    k = { '<C-w>k<cmd>WhichKey <LT>Leader>w<CR>', 'Go up' },
    h = { '<C-w>h<cmd>WhichKey <LT>Leader>w<CR>', 'Go left' },
    l = { '<C-w>l<cmd>WhichKey <LT>Leader>w<CR>', 'Go right' },

    d = { '<C-w>c<cmd>WhichKey <LT>Leader>w<CR>', 'Delete' },

    s = { '<C-w>s<cmd>WhichKey <LT>Leader>w<CR>', 'Split horizontal' },
    v = { '<C-w>v<cmd>WhichKey <LT>Leader>w<CR>', 'Split vertical' },
},
...

Let me know if there is a better way.

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.