How do I add a description to a Neovim keymapping so it appears in which-key and :map output?
Answer
vim.keymap.set('n', '{key}', {fn}, { desc = '{description}' })
Explanation
When defining keymaps with vim.keymap.set(), passing a desc field in the options table attaches a human-readable description to the mapping. This description appears in :map output and is automatically picked up by plugins like which-key.nvim — no extra configuration needed.
How it works
vim.keymap.set(mode, lhs, rhs, opts)— Neovim's standard Lua mapping functionopts.desc— a string describing what the mapping does:map <key>— verifies the description is attached; it appears in the rightmost columnvim.keymap.get()— programmatically retrieves mapping info including the description
Example
Without description:
vim.keymap.set('n', '<leader>ff', telescope.find_files)
With description:
vim.keymap.set('n', '<leader>ff', telescope.find_files, {
desc = 'Find files',
})
Now :nmap <leader>ff shows Find files alongside the binding. which-key automatically uses this as the label for <leader>ff.
Tips
- Keep descriptions short (2–4 words) so they fit in which-key's popup
- Prefix related mappings for clarity:
'LSP: hover','LSP: rename','LSP: references' :Telescope keymaps— search all described mappings interactively if using Telescope- Descriptions are stored in the same table as
noremap,silent, andbufferopts — no separate call needed