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

How do I jump to the next LSP diagnostic error or warning in Neovim?

Answer

vim.diagnostic.goto_next()

Explanation

Neovim's built-in vim.diagnostic module provides functions for navigating LSP diagnostics (errors, warnings, hints) without any plugin. Mapping vim.diagnostic.goto_next() and vim.diagnostic.goto_prev() lets you hop between diagnostics the same way ]e / [e hop between quickfix entries, keeping your hands on the keyboard throughout the review workflow.

How it works

  • vim.diagnostic.goto_next() — jump to the next diagnostic after the cursor (wraps around)
  • vim.diagnostic.goto_prev() — jump to the previous diagnostic before the cursor
  • vim.diagnostic.open_float() — open a floating window with the full diagnostic message at the cursor
  • Both accept an options table: { severity = vim.diagnostic.severity.ERROR } to filter by severity

Example

Add these mappings to your Neovim config:

vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Next diagnostic' })
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Previous diagnostic' })
vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, { desc = 'Show diagnostic' })

-- Jump only to errors, skipping warnings and hints
vim.keymap.set('n', ']E', function()
  vim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR })
end, { desc = 'Next error' })

Tips

  • vim.diagnostic.get(0) returns all diagnostics for the current buffer as a table you can inspect with Lua
  • vim.diagnostic.setloclist() populates the location list with all diagnostics for easy :lnext navigation
  • vim.diagnostic.config({ virtual_text = false }) disables inline virtual text if it clutters your view
  • These functions work with any diagnostic source (LSP, linters via null-ls, etc.) not just language servers

Next

How do I jump to a tag in Vim and automatically choose if there is only one match?