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

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 elsewhere
  • cmd — the command and arguments used to launch the language server executable
  • root_markers — files or directories whose presence marks the project root; Neovim walks upward from the current file to find them
  • filetypes — 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_markers field replaces the root_dir function from nvim-lspconfig for most setups
  • You can still override root_dir with a function for complex projects: root_dir = function(fname) return vim.fs.root(fname, {'.git'}) end
  • Use :LspInfo to inspect which servers are active in the current buffer

Next

What is the difference between the inner word (iw) and inner WORD (iW) text objects in Vim?