# Vim LSP Not Working
Language Server Protocol (LSP) provides modern IDE features like autocomplete, go-to-definition, and diagnostics. But setting it up can be tricky. If LSP isn't working in Vim, here's how to debug and fix it.
Understanding LSP Components
LSP requires:
- 1.LSP Client - Plugin in Vim that talks to language servers
- 2.Language Server - External program (like
pyright,tsserver) - 3.Configuration - Connecting client to server
Check LSP Status
Neovim (Native LSP)
:LspInfo
:LspLogCheck if server is attached:
:lua print(vim.lsp.get_active_clients()[1].name)coc.nvim
:CocInfo
:CocList servicesCheck language server status:
:CocCommand languageserver.checkvim-lsp
:LspStatus
:LspLogLanguage Server Not Starting
Server Not Installed
Install the language server for your language:
```bash # Python pip install pyright # Or pip install python-lsp-server
# TypeScript/JavaScript npm install -g typescript typescript-language-server
# Go go install golang.org/x/tools/gopls@latest
# Rust rustup component add rust-analyzer
# C/C++ brew install llvm # macOS # Or install clangd from system package manager ```
Server Not in PATH
Check if server is executable:
which pyright
which tsserver
which goplsIf not found, either install or add to PATH.
Configuration Missing
The LSP client needs configuration to start servers.
#### Neovim Native LSP
```lua local lspconfig = require('lspconfig')
lspconfig.pyright.setup{} lspconfig.tsserver.setup{} lspconfig.gopls.setup{} ```
#### coc.nvim
// In coc-settings.json
{
"languageserver": {
"python": {
"command": "pyright",
"filetypes": ["python"]
}
}
}#### vim-lsp
if executable('pyright')
au User lsp_setup call lsp#register_server({
'name': 'pyright',
'cmd': {server_info->['pyright']},
'whitelist': ['python'],
})
endifNo Completion Appearing
Completion Not Configured
For Neovim, you need a completion plugin:
local cmp = require('cmp')
cmp.setup({
sources = {
{ name = 'nvim_lsp' },
},
mapping = {
['<Tab>'] = cmp.mapping.select_next_item(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
}
})Trigger Character Not Hit
Some completion requires trigger characters. Check:
:lua print(vim.lsp.get_active_clients()[1].capabilities)Completion Disabled
Check completion settings:
:set completefunc?
:set omnifunc?Should show something like v:lua.vim.lsp.omnifunc.
No Diagnostics Showing
Diagnostics Disabled
Neovim:
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics, {
virtual_text = true,
signs = true,
}
)No Errors in File
The file might not have errors. Check with:
:lua print(#vim.lsp.diagnostic.get(0))Sign Column Hidden
Enable sign column:
set signcolumn=yesGo-To-Definition Not Working
No Reference Data
Language server might not have parsed the file. Wait for initialization.
Check if server supports it:
:lua print(vim.lsp.get_active_clients()[1].resolved_capabilities.goto_definition)Key Mapping Missing
Set up key mappings:
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, {})
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, {})
vim.keymap.set('n', 'gr', vim.lsp.buf.references, {})For coc.nvim:
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gr <Plug>(coc-references)LSP Server Crashes
Server Memory Limits
Some servers (especially for large projects) need memory:
lspconfig.pyright.setup{
settings = {
python = {
analysis = {
memory = 4096,
}
}
}
}Server Timeout
Increase timeout:
lspconfig.tsserver.setup{
cmd = {'typescript-language-server', '--stdio'},
init_options = {
maxTsServerMemoryLimit = 4096,
}
}Check Logs
:LspLog
:e ~/.cache/nvim/lsp.logMultiple Servers Conflict
If multiple servers attach to same buffer:
:lua print(vim.inspect(vim.lsp.get_active_clients()))Filter by filetype:
lspconfig.pyright.setup{
filetypes = {'python'},
}Project Root Detection
LSP needs to find project root. If it doesn't:
lspconfig.tsserver.setup{
root_dir = lspconfig.util.root_pattern('package.json', 'tsconfig.json', '.git'),
}Or specify manually:
lspconfig.pyright.setup{
root_dir = function()
return vim.fn.getcwd()
end
}Workspace Configuration
Some servers need workspace settings:
lspconfig.rust_analyzer.setup{
settings = {
['rust-analyzer'] = {
checkOnSave = {
command = 'clippy',
},
}
}
}Slow LSP Response
File Parsing Delay
Large files take time to parse. Be patient after opening.
Reduce Analysis
lspconfig.pyright.setup{
settings = {
python = {
analysis = {
autoSearchPaths = false,
diagnosticMode = 'openFilesOnly',
}
}
}
}Disable Hover
vim.lsp.handlers['textDocument/hover'] = function() endcoc.nvim Specific Issues
Extension Not Installed
:CocInstall coc-pyright
:CocInstall coc-tsserver
:CocInstall coc-goExtension Disabled
:CocList extensionsEnable disabled extensions:
:CocEnableExtension coc-pyrightConfiguration Wrong
Edit settings:
:CocConfigCheck JSON is valid:
:CocInfovim-lsp Specific Issues
Server Not Registered
:LspStatusIf empty, check registration:
:echo lsp#get_allowed_servers()Auto-Start Disabled
let g:lsp_auto_enable = 1LSP Debug Checklist
- 1.Check server installed:
which pyright - 2.Check server runs:
pyright --version - 3.Check LSP client loaded:
:LspInfo - 4.Check server attached:
:lua print(vim.lsp.get_active_clients()) - 5.Check logs:
:LspLog - 6.Check completion source:
:lua print(vim.inspect(require('cmp').get_config().sources)) - 7.Check diagnostics:
:lua print(#vim.lsp.diagnostic.get(0)) - 8.Check root directory detected
- 9.Check filetype matches server whitelist
- 10.Check key mappings exist
Complete Neovim LSP Setup
```lua -- Install lspconfig local lspconfig = require('lspconfig')
-- Setup servers lspconfig.pyright.setup{} lspconfig.tsserver.setup{} lspconfig.gopls.setup{}
-- Completion local cmp = require('cmp') cmp.setup({ sources = { { name = 'nvim_lsp' } }, mapping = { ['<Tab>'] = cmp.mapping.select_next_item(), ['<CR>'] = cmp.mapping.confirm({ select = true }), } })
-- Keymaps vim.keymap.set('n', 'gd', vim.lsp.buf.definition) vim.keymap.set('n', 'K', vim.lsp.buf.hover) vim.keymap.set('n', '<leader>r', vim.lsp.buf.rename)
-- Diagnostics vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with( vim.lsp.diagnostic.on_publish_diagnostics, { virtual_text = true, signs = true, } ) ```
Quick Reference
```vim " Neovim native LSP :LspInfo :LspLog :LspStart :LspStop
" coc.nvim :CocInfo :CocInstall coc-pyright :CocConfig
" vim-lsp :LspStatus :LspLog
" Check clients :lua print(vim.inspect(vim.lsp.get_active_clients()))
" Keymaps (Neovim) gd " Go to definition K " Hover gr " References <leader>r " Rename ```
LSP setup requires installing servers, configuring clients, and connecting them. Check each component systematically when issues arise.