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.38k stars 177 forks source link

feature: Register group names without bindings #514

Closed theherk closed 3 months ago

theherk commented 1 year ago

Did you check the docs?

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

I'm guessing like many other things this is possible, and I'm overlooking it. In neorg for example there are some default keys set for localleader using another method. Their key descriptions are picked up correctly by which key, but there are no labels.

Describe the solution you'd like

What I'd like to do is simply:

                local wk = require("which-key")
                wk.register({
                  ["<localleader>"] = {
                    i = { name = "+insert", },
                    l = { name = "+list", },
                    m = { name = "+mode", },
                    n = { name = "+note", },
                    t = { name = "+mark", },
                  },
                })

But this doesn't seem to work without any other bindings contained. If I add even one, it does work and the remaining defaults are intact, but that is a bit redundant.

Describe alternatives you've considered

The alternative I use, is simply setting all the mappings again:

                local wk = require("which-key")
                wk.register({
                  ["<localleader>"] = {
                    i = {
                      name = "+insert",
                    },
                    l = {
                      name = "+list",
                    },
                    m = {
                      name = "+mode",
                    },
                    n = {
                      name = "+note",
                    },
                    t = {
                      name = "+mark",
                      a = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_ambiguous<cr>", "ambiguous" },
                      c = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_cancelled<cr>", "cancelled" },
                      d = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_done<cr>", "done" },
                      h = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_on_hold<cr>", "hold" },
                      i = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_important<cr>", "important" },
                      p = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_pending<cr>", "pending" },
                      r = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_recurring<cr>", "recurring" },
                      u = { "<cmd>Neorg keybind norg core.qol.todo_items.todo.task_undone<cr>", "undone" },
                    },
                  },
                })

Additional context

I've run into this elsewhere too; where a plugin provides default keys with perfect descriptions, but I don't seem to be able to register the groups containing them without resetting the bindings.

freddiehaddad commented 1 year ago

Not sure if my scenario is the same as yours, but I have some git/lsp keymaps that don't get set until I open the file and start the LSP server, etc. For those keymaps, I register them for the buffer using an autocmd:

Simplified example:

vim.api.nvim_create_autocmd(
    'LspAttach',
    { callback = function(ev)
        -- define your keymaps here
        vim.keymap.set(...)

        -- register the buffer local keymaps
        require('which-key').register({ ['<leader>f' = { name = '+foo' } }, { buffer = ev.buf })
    end
})

I may be misunderstanding your issue. For me, the empty menu items wouldn't show up until I added keymaps and the the name was missing and would show '+prefix' instead.

sungyoonc commented 7 months ago

I've come around using this workaround. When I want to register a group name, I make a dummy binding with a key that I will never press like this unicode.

wk.register({
    ["<localleader>"] = {
        s = { name = "+send", ["🚫"] = "which_key_ignore" },
        v = { name = "+view", ["🚫"] = "which_key_ignore" },
    },
})
github-actions[bot] commented 4 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.