Skip to content
Vim

LSP, Completion & Diagnostics

Get IDE features (autocomplete, go-to-definition, diagnostics) via LSP.

By EZ4Code Team
lspcompletiondiagnosticsneovim

Code

" --- Neovim native LSP (built-in client) ---
" Install servers: npm i -g typescript-language-server pyright vscode-langservers-extracted
:help lsp

lua << EOF
local lsp = require("lspconfig")
lsp.ts_ls.setup{}        -- TypeScript/JavaScript
lsp.pyright.setup{}      -- Python
lsp.lua_ls.setup{}       -- Lua
lsp.rust_analyzer.setup{} -- Rust
EOF

" Keymaps (set in on_attach)
nnoremap gd        <cmd>lua vim.lsp.buf.definition()<CR>
nnoremap K         <cmd>lua vim.lsp.buf.hover()<CR>
nnoremap gr        <cmd>lua vim.lsp.buf.references()<CR>
nnoremap <leader>rn <cmd>lua vim.lsp.buf.rename()<CR>
nnoremap <leader>ca <cmd>lua vim.lsp.buf.code_action()<CR>
nnoremap <leader>d  <cmd>lua vim.diagnostic.open_float()<CR>

" Completion: nvim-cmp or native (Neovim 0.11+)
set completeopt=menuone,noselect

" Diagnostics
lua << EOF
vim.diagnostic.config({
  virtual_text = true,
  signs = true,
  underline = true,
})
EOF

" Format on save (Neovim 0.8+)
autocmd BufWritePre *.lua,*.ts,*.py lua vim.lsp.buf.format({ async = false })

Explanation

LSP (Language Server Protocol) gives Vim/Neovim IDE features. Neovim ships a built-in client (vim.lsp); classic Vim needs coc.nvim or ale. You install language servers separately (typescript-language-server, pyright, gopls, rust-analyzer...) and configure each with lspconfig. Core actions: gd (definition), K (hover), gr (references), <leader>ca (code action), <leader>rn (rename). For completion, nvim-cmp with luasnip is the modern stack; Neovim 0.11+ also has a native omnifunc. Diagnostics can show as virtual text, signs, or underlines.

More Vim Snippets