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
5.25k stars 165 forks source link

bug/feature: partial mapping (aliases) don't work as expected #583

Closed MattSturgeon closed 2 months ago

MattSturgeon commented 7 months ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

When I map "<leader>w" to "<C-w>", I don't get the which-key popup for <C-w> when typing <leader>w.

Describe the solution you'd like

"Alias" mappings should be detected by which-key and displayed

Describe alternatives you've considered

Alternatively, "aliases" could be manually configured during plugin setup.

e.g.

operators = { --[[ ... ]] },
key_labels = { --[[ ... ]] },
key_aliases = {
  ["<C-w>"] = { "<leader>w", "gw" },
},

Additional context

Extra ideas:

adrianchongseek commented 7 months ago

You can try something like this:

return {
  'folke/which-key.nvim',
  config = function()
    local wk = require('which-key')
    wk.register({
      ["<leader>"] = {
        w = {
          '<cmd>WhichKey <C-w><cr>', 'Window actions'
        },
      },
      ["gw"] = {
        '<cmd>WhichKey <C-w><cr>', 'Window actions'
      }
    })
  end
}

This is not perfect, but it will work like you expect.

MattSturgeon commented 7 months ago

You can try something like this: `["<leader>w"] = { '<cmd>WhichKey <C-w><cr>', 'Window actions' }

This is not perfect, but it will work like you expect.

Thanks, calling :WhichKey [[action]] manually is a decent workaround, other than which-key opening instantly (without any delay) it seems to achieve what I wanted.

I'll leave this issue open so that @folke can consider the underlying issue, feature request and/or adding the workaround example to the docs (etc).

github-actions[bot] commented 3 months 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.

MattSturgeon commented 3 months ago

I'd still like to find a better solution, but I don't know what that might be...

MattSturgeon commented 2 months ago

Thanks! Was 4537d3ea52b2b11b96ca2fdde2bb4573f0ca7c73 the fix for this one?

MattSturgeon commented 2 months ago

For anyone else looking, the correct way to configure this now is:

require('which-key').add({
  { "<leader>w", proxy = "<C-w>", group = "window" }
})
mattcoleanderson commented 1 month ago

For anyone else looking, the correct way to configure this now is:

require('which-key').add({
  { "<leader>w", proxy = "<C-w>", group = "window" }
})

So this fixes the problem to a degree. When directly using the <C-w> command, every possible command isn't included in the popup. For example, <C-w> c to close a window isn't. However, you can still use it.

With this proxy solution only commands that are in the popup work, so <leader>wc does not work.

MattSturgeon commented 1 month ago

With this proxy solution only commands that are in the popup work, so <leader>wc does not work.

Can confirm, I've also noticed this behaviour.

I have yet to troubleshoot whether it is a vim or a which-key issue, though. I.e. if you had a :map <leader>w <C-w> mapping, would <leader>wc work correctly?

If this is an problem specific to which-key's proxy mappings, I think we ought to open a new issue to track it, though.

mattcoleanderson commented 1 week ago

If this is an problem specific to which-key's proxy mappings, I think we ought to open a new issue to track it, though.

I've figured out why this is and how to fix it. In the codebase there is a mapping called window with a set of presets when pressing <leader>w. Not every command useable by <C-W> is in this explicit mapping found at the following link:

https://github.com/folke/which-key.nvim/blob/fb070344402cfc662299d9914f5546d840a22126/lua/which-key/plugins/presets.lua#L100-L122

However, you can add more keymaps to the window group by defining a proxy keymap. Here is a snippet of some I've added to the windows group:

wk.add({
  { '<leader>w', proxy = '<c-w>', group = 'window' }, -- proxy to window mappings
  { '<c-w>c',    desc = 'Close window' },
  { '<c-w>H',    desc = 'move current window to the far left' },
  { '<c-w>J',    desc = 'move current window to the very bottom' },
  { '<c-w>K',    desc = 'move current window to the very top' },
  { '<c-w>L',    desc = 'move current window to the far right' },
})