How do I configure a language server in Neovim 0.11 without the nvim-lspconfig plugin?
Answer
vim.lsp.config('server', { cmd = {...}, root_markers = {...}, filetypes = {...} })
Explanation
Neovim 0.11 introduced vim.lsp.config() as a built-in way to define language server configurations without requiring the nvim-lspconfig plugin. You call it once per server in your Lua configuration, specifying the server command, the root markers that identify the project root, and the filetypes that should trigger the server.
How it works
'clangd'— the logical name used to reference this server configuration elsewherecmd— the command and arguments used to launch the language server executableroot_markers— files or directories whose presence marks the project root; Neovim walks upward from the current file to find themfiletypes— the file types that trigger this server when a buffer is opened
Example
vim.lsp.config('clangd', {
cmd = { 'clangd', '--background-index' },
root_markers = { 'compile_commands.json', '.clangd', '.git' },
filetypes = { 'c', 'cpp' },
})
Place this in ~/.config/nvim/lsp/clangd.lua — Neovim 0.11 automatically loads files from lsp/ in your config directory. Then call vim.lsp.enable('clangd') to activate it.
Tips
- Each server name must be unique and match the string passed to
vim.lsp.enable() - The
root_markersfield replaces theroot_dirfunction from nvim-lspconfig for most setups - You can still override
root_dirwith a function for complex projects:root_dir = function(fname) return vim.fs.root(fname, {'.git'}) end - Use
:LspInfoto inspect which servers are active in the current buffer