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

bug: Mappings inconsistent over different modes #531

Closed MarcoBuess closed 3 months ago

MarcoBuess commented 1 year ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.9.1

Operating system/version

Windows 10 (10.0.19045)

Describe the bug

Given the following mappings:

M.wk_mappings = {
    f = { "[F]ind" },
    c = { name = "[C]ode" },
    w = { "[W]orkplace" },
    t = {
        name = "[T]erminal",
        s    = { "[S]end" },
    },
}

and the following set of keybindings:

    map("n", "<leader>tsl", ":SendToTerminal line<CR>", default_opts, "[L]ine")
    map("n", "<leader>tsb", ":SendToTerminal buffer<CR>", default_opts, "[B]uffer")
    map("v", "<leader>tss", ":SendToTerminal selection<CR>", default_opts, "[S]election")

The normal mode mappings will work fine. Once I have a visual selection and trigger which-key with <leader> the labels for t don't get applied. They will show but only with the name +prefix for t and s respectively. What am I missing?

Steps To Reproduce

  1. Take the aforementioned mappings
  2. Highlight text in visual mode
  3. Trigger which-key with <leader>

Expected Behavior

Shows the same label names as in normal mode.

Repro

No response

MarcoBuess commented 1 year ago

This comment gave the right hint. I made to following updates to my bindings:

M.wk_register_n = {
    f = { "[F]ind" },
    c = { name = "[C]ode" },
    w = { "[W]orkplace" },
    t = {
        name = "[T]erminal",
        s    = { "[S]end" },
    },
}

M.wk_register_v = {
    t = {
        name = "[T]erminal",
        s    = { "[S]end" },
    },
}

and updated my spec like so:

{
    "folke/which-key.nvim",
    event = "VeryLazy",
    opts = {
        plugins = {
            spelling = { enabled = true },
        },
    },
    config = function()
        require("which-key").register({ require("config.keymaps").wk_register_n },
            { mode = "n", prefix = "<leader>" })
        require("which-key").register({ require("config.keymaps").wk_register_v },
            { mode = "v", prefix = "<leader>" })
    end
}

Labels are now showing as expected. Is this how I'm supposed to do this?

MarcoBuess commented 1 year ago

For some reason it is more consistent if you're binding without explicit prefix but rather using ["<leader>"] directly in the binding. Then if setting mode { "n", "v" } it's binding in both modes as expected. I guess thats why it is also done this way in LazyVim?

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.