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.99k stars 160 forks source link

feature: Possibility to use a keybinding with different arguments each time #520

Closed zellidev0 closed 3 weeks ago

zellidev0 commented 10 months ago

Did you check the docs?

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

I'm trying to register a keybinding for IncRename but this general question came up.

When the cursor is on a LSP identifier and I'm entering the command :IncRename xxx it renames the word under the cursor to xxx while highlighting it in the buffer. The problem is xxx of course is a custom name for a LSP identifier that changes when running the command for another variable, function etc. Which-key can (to my limited knowledge) only register a final command that does not get arguments of any kind.

I tried registering it in different ways but I can never enter a unique argument when running the command each time.

wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rvariant1"] = { "<Cmd>IncRename", "Rename" }, -- not working due to missing <CR>
  ["<leader>rvariant2"] = { -- not working because no argument is passed
    "<Cmd>lua vim.api.nvim_command(':IncRename ' )<CR>" , "Rename current word"
  },
})

Describe the solution you'd like

I'm looking for a way to register a command that receives an argument so I can press the keybinding and simply start typing then. Like so (naive approach with pseudo code):

wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rvariant1"] = { "<Cmd>IncRename <ARG1><CR>", "Rename" },
})

Where is unique in each run of the command.

Describe alternatives you've considered

My current alternative is to register the keybinding like so: And use which-key only for the documentation.

vim.keymap.set("n", "<leader>rn", function()
  return ":IncRename " .. vim.fn.expand("<cword>")
end, { expr = true })
wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rr"] = { "Rename" },
})

Additional context

None, but thanks for the help.

github-actions[bot] commented 1 month 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.