vimtricks.wiki Concise Vim tricks, one at a time.

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

  • gra fires vim.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, gra applies 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/codeAction capability
  • Companion keymaps added in Neovim 0.10: grr (rename), gri (go to implementation), gO (document symbols)
  • If you were previously mapping <leader>ca to vim.lsp.buf.code_action, you can now rely on gra directly
  • Use :checkhealth lsp to verify your LSP client is attached and supports code actions

Next

How do I make Neovim restore the scroll position when navigating back through the jump list?