Registry Mirror
Skip to content

Fix which-key errors #483

Open
Open

Description

@NeilGirdhar

I was forced to do this to suppress the which-key warnings:

    -- Commenting
    {
        'numToStr/Comment.nvim',
        config = function()
            require('Comment').setup()
            vim.keymap.del("n", "gc")
            vim.keymap.del("n", "gb")
            local wk = require('which-key')
            wk.add({
                { "gb", group = "Comment toggle blockwise" },
                { "gc", group = "Comment toggle linewise" },
            })
        end
    },

Activity

  1. thomastthai commented on Oct 1, 2024

    @thomastthai

    I landed here when I found which-key.nvim was causing Comment.nvim gcc to not work. I'm still trying to solve the issue.

    Which warning did you get from which-key? How does your snippet remove the warning? I see that "gc" and "gb" keymaps in normal mode are removed. Those keymaps are added back via wk.add()?

  2. NeilGirdhar commented on Oct 1, 2024

    @NeilGirdhar
    Author

    Yes because the the mappings in Comment.nvim are ordinary mappings rather than groups. This conflicts with nested mappings. This snippet removes the bad mappings and replaces them with groups, which doesn't conflict.

  3. thomastthai commented on Oct 2, 2024

    @thomastthai

    Perhaps I misunderstood something. require('Comment').setup() sets the default keymaps for Comment.nvim that include gc and gb. Then vim.keymap.del("n", "gc") and vim.keymap.del("n", "gb") delete those default keymaps. For those keymaps to work again, wouldn't each keymap in wk.add() need additional RHS parameters?

    Some RHS parameters from Comment.txt at around line 267:

    Usage: ~
    >lua
        -- Toggle current line or with count
        vim.keymap.set('n', 'gcc', function()
            return vim.v.count == 0
                and '<Plug>(comment_toggle_linewise_current)'
                or '<Plug>(comment_toggle_linewise_count)'
        end, { expr = true })
    
        -- Toggle in Op-pending mode
        vim.keymap.set('n', 'gc', '<Plug>(comment_toggle_linewise)')
    
        -- Toggle in VISUAL mode
        vim.keymap.set('x', 'gc', '<Plug>(comment_toggle_linewise_visual)')
    <
    
  4. NeilGirdhar commented on Oct 2, 2024

    @NeilGirdhar
    Author

    For those keymaps to work again,

    Those keymaps don't need to "work" since they're supposed to be group-keymaps. We don't want to run those plug-commands. We want to run nothing and set the which-key heading. gc should do this:

    Screenshot 2024-10-02 at 4 39 26 PM

    Some RHS parameters from Comment.txt at around line 267:

    Yes, those look like the problematic lines.

  5. thomastthai commented on Oct 2, 2024

    @thomastthai

    For those coming here from the future, the which-key warnings @NeilGirdhar may be referring to are from :checkhealth which-key are similar to:

    - WARNING In mode `n`, <gb> overlaps with <gbc>:
      - <gb>: Comment toggle blockwise
      - <gbc>: Comment toggle current block
    - WARNING In mode `n`, <gc> overlaps with <gco>, <gcA>, <gcc>, <gcO>:
      - <gc>: Comment toggle linewise
      - <gco>: Comment insert below
      - <gcA>: Comment insert end of line
      - <gcc>: Comment toggle current line
      - <gcO>: Comment insert above
    - WARNING In mode `n`, <<Space>gc> overlaps with <<Space>gco>, <<Space>gcA>, <<Space>gcc>, <<Space>gcO>:
      - <<Space>gc>: Comment toggle linewise
      - <<Space>gco>: Comment insert below
      - <<Space>gcA>: Comment insert end of line
      - <<Space>gcc>: Comment toggle current line
      - <<Space>gcO>: Comment insert above
    - WARNING In mode `n`, <<Space>gb> overlaps with <<Space>gbc>:
      - <<Space>gb>: Comment toggle blockwise
      - <<Space>gbc>: Comment toggle current block
    

    I changed my keymaps for Comment.nvim to use my key which is , which may be different than yours.

  6. thomastthai commented on Oct 2, 2024

    @thomastthai

    @NeilGirdhar , thanks for taking the time to explain. I'll summarize to help others new to which-key, nvim, or Comment.nvim save time down the rabbit hole.

    I made many configuration changes during my experiments, including using gb and gc as keymaps, not just group-keymaps! So in my head, I kept thinking if they get deleted, they can't be used again. For the default settings, Comment.nvim sets gc and gb as "ordinary mappings rather than groups." gcc and gbc are "ordinary" keymaps. Therefore, deleting gc and gb, and re-adding them as group-keymap via wk.add() removes the warnings, and gcc and gbc continue to work.

  7. NeilGirdhar commented on Oct 2, 2024

    @NeilGirdhar
    Author

    You got it! Do you feel like trying to get a pull request through to remove these from Comment.nvim? Maybe even (somehow, I have no idea how) make it add the group bindings if which-key is installed? That would clean this up for all users 😄

  8. thomastthai commented on Oct 3, 2024

    @thomastthai

    Having Comment.nvim detect if which-key is in use and addressing the issue within the Comment.nvim's source code would be idea. I am still new to Lua and nvim to be able to get a pull request though to fix this.

  9. bak commented on Oct 6, 2024

    @bak

    I'm curious if either of you updated your which-key config for v3 — I found the :checkhealth which-key warnings for all my mappings (it was complaining about others besides Comment.nvim) went away after I updated the config as described here: https://github.com/folke/which-key.nvim#-installation.

  10. NeilGirdhar commented on Oct 6, 2024

    @NeilGirdhar
    Author

    @bak Which installation option are you talking about? I get

    checking for overlapping keymaps ~
    - WARNING In mode `n`, <gc> overlaps with <gcO>, <gcc>, <gco>, <gcA>:
      - <gc>: Comment toggle linewise
      - <gcO>: Comment insert above
      - <gcc>: Comment toggle current line
      - <gco>: Comment insert below
      - <gcA>: Comment insert end of line
    - WARNING In mode `n`, <gb> overlaps with <gbc>:
      - <gb>: Comment toggle blockwise
      - <gbc>: Comment toggle current block
    - OK Overlapping keymaps are only reported for informational purposes.
      This doesn't necessarily mean there is a problem with your config.
    

    without the above configuration.

  11. bak commented on Oct 8, 2024

    @bak

    Yeah sorry I don't know exactly the change that fixed things, but I was getting the same output as you, and found this thread, but noticed that which-key released 3.0 about the time of your first post and went to check out their README to see what might have changed. I replaced what I had with their suggested config:

    {
      "folke/which-key.nvim",
      event = "VeryLazy",
      opts = {
        -- your configuration comes here
        -- or leave it empty to use the default settings
        -- refer to the configuration section below
      },
      keys = {
        {
          "<leader>?",
          function()
            require("which-key").show({ global = false })
          end,
          desc = "Buffer Local Keymaps (which-key)",
        },
      },
    }
    

    and haven't had a problem since

  12. NeilGirdhar commented on Oct 8, 2024

    @NeilGirdhar
    Author

    I don't think that works. Try removing the config and see if the errors reappear. If not, then this isn't really a fix.

  13. Edditoria commented on Mar 1, 2025

    @Edditoria
            vim.keymap.del("n", "gc")
            vim.keymap.del("n", "gb")
    

    It will lose the ability of motions, e.g. gc2j to comment 3 lines.

  14. NeilGirdhar commented on Mar 1, 2025

    @NeilGirdhar
    Author

    It will lose the ability of motions, e.g. gc2j to comment 3 lines.

    What do you mean?

  15. Edditoria commented on Mar 1, 2025

    @Edditoria

    @NeilGirdhar If add the lines in my nvim config, I cannot comment 3 lines by typing "gc2j" (This is like an example). There is a trade-off, right?

    I'm also investigating this warning from :checkhealth which-key. However, for Comment.nvim, there is a reason to set the keymap "gc" and "gb". In the meantime, I couldn't find a way to solve this warning by changing code in Comment.nvim :-/

  16. NeilGirdhar commented on Mar 1, 2025

    @NeilGirdhar
    Author

    @Edditoria You are right! Any ideas on what a good resolution would be? Can which-key be improved to show a menu entry for "movement commands do this"?

  17. added a commit that references this issue on Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions