How do I trigger a code action at the cursor position using Neovim's built-in LSP?
Answer
gra
Explanation
Since Neovim 0.10, gra is a default LSP keymap that calls vim.lsp.buf.code_action(). It opens a list of code actions available at the cursor position — such as auto-imports, quick fixes, refactors, and extract functions — without requiring any plugin or manual mapping setup.
How it works
grafiresvim.lsp.buf.code_action()when an LSP client is attached to the buffer- Neovim presents the available actions in a selection UI (typically a floating list)
- Select an action with the arrow keys and press
<CR>to apply it - In visual mode,
graapplies code actions to the visually selected range
Example
In a Python file with pyright attached:
import os ← cursor here, os is unused
Press: gra
Options appear:
1. Remove unused import 'os'
2. Ignore this warning
Choose option 1 to remove the import automatically.
Tips
- Works with any LSP server that supports the
textDocument/codeActioncapability - Companion keymaps added in Neovim 0.10:
grr(rename),gri(go to implementation),gO(document symbols) - If you were previously mapping
<leader>catovim.lsp.buf.code_action, you can now rely ongradirectly - Use
:checkhealth lspto verify your LSP client is attached and supports code actions