Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท Neovim and Lua

LSP, Tree-sitter and Plugin Managers

Turn Neovim into an IDE-style editor: lazy.nvim plugin specs, the built-in LSP client, Tree-sitter highlighting, completion, fuzzy finding and what to install first.

Neovim becomes a full development environment with three ingredients: a plugin manager to install things, the built-in LSP client for language features, and Tree-sitter for accurate syntax trees. This lesson explains each, shows a realistic setup and teaches how to keep it small.

All snippets are read-only, written for current Neovim (0.11 or newer, where vim.lsp.config() and vim.lsp.enable() exist) and for the plugin APIs as of writing. Plugin options change over time, so always compare with the plugin's own README. The stand-ins are runnable JavaScript models of the ideas.

lazy.nvim, a Lua plugin manager

Most Neovim setups use lazy.nvim, which installs plugins declared as Lua tables and loads them lazily (on an event, a command, a key or a file type). It needs a bootstrap snippet in init.lua that clones it once:

LUA
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable",
    "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
  { "nvim-telescope/telescope.nvim", dependencies = { "nvim-lua/plenary.nvim" },
    cmd = "Telescope",
    keys = { { "<leader>ff", "<cmd>Telescope find_files<cr>", desc = "Find files" } } },
  { "folke/tokyonight.nvim", priority = 1000,
    config = function() vim.cmd.colorscheme("tokyonight") end },
})

Each entry is a table whose first item is the GitHub owner/repo. The useful fields:

Field Meaning
dependencies Other plugins to load first
build Command run after install or update
config Function (or opts = {}) that sets the plugin up
event, cmd, ft, keys Lazy-load triggers
lazy = false Load at startup
priority Load order for startup plugins (themes use a high number)
opts Table passed to the plugin's setup()

Commands: :Lazy opens the UI (install, update, profile) and :Lazy sync installs, updates and cleans. A lockfile lazy-lock.json pins versions; commit it so all machines match. Other managers exist (vim.pack is being added to Neovim itself, packer is archived, mini.deps), and native packages from the plugins lesson still work.

JS Stand-in: lazy-loading triggers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The built-in LSP client

The Language Server Protocol separates editors from language intelligence. A language server (a separate program such as lua-language-server, pyright, typescript-language-server, gopls, rust-analyzer or clangd) analyzes your code. The editor is the client: it sends file contents and receives diagnostics, hover text, definitions, references, completions, renames and code actions.

sequenceDiagram
    participant N as Neovim (client)
    participant S as Language server
    N->>S: initialize, didOpen (file text)
    S-->>N: diagnostics
    N->>S: textDocument/definition (cursor position)
    S-->>N: location
    N->>N: jump to the file and line

The server runs as a separate process, usually talking over stdin and stdout.

Install the servers you need with your OS package manager, npm, pip or a helper such as mason.nvim, which downloads many of them. In Neovim 0.11 and later, configure and start one like this:

LUA
vim.lsp.config("lua_ls", {
  cmd = { "lua-language-server" },
  filetypes = { "lua" },
  root_markers = { ".luarc.json", ".git" },
  settings = { Lua = { diagnostics = { globals = { "vim" } } } },
})
vim.lsp.enable({ "lua_ls", "pyright", "ts_ls" })

nvim-lspconfig is a plugin with ready-made definitions for hundreds of servers, so many configs only need vim.lsp.enable("name") after installing it. When a server attaches to a buffer, the LspAttach autocommand fires; that is the place for buffer-local keymaps and options.

Default keymaps and your own

Recent Neovim versions define defaults when an LSP attaches: K hover, grn rename, gra code action, grr references, gri implementation, gO document symbols, Ctrl-S signature help (insert mode), ]d/[d next/previous diagnostic and Ctrl-], Ctrl-w d for definition and diagnostic float. Add your own where useful:

LUA
vim.api.nvim_create_autocmd("LspAttach", {
  callback = function(args)
    local o = { buffer = args.buf }
    vim.keymap.set("n", "gd", vim.lsp.buf.definition, o)
    vim.keymap.set("n", "<leader>f", function() vim.lsp.buf.format({ async = true }) end, o)
  end,
})
vim.diagnostic.config({ virtual_text = true, severity_sort = true })

Check status with :checkhealth vim.lsp and :LspInfo (provided by nvim-lspconfig on older setups). If nothing happens, the usual causes are: the server is not installed or not on PATH, the file type is wrong, or no root marker was found.

Completion comes from the server too. Since 0.11 you can enable the native completion (vim.lsp.completion.enable), or install a plugin such as blink.cmp or nvim-cmp for a richer menu with snippets and extra sources.

Tree-sitter

A parser converts source text into a syntax tree, and it updates incrementally as you type. Neovim uses it for highlighting, folds, indentation and text objects. Highlighting based on a real tree is accurate where regular-expression syntax files guess: it knows a name is a function parameter, not just "some word".

Neovim includes the Tree-sitter library; the grammars come from the nvim-treesitter plugin:

LUA
require("nvim-treesitter.configs").setup({
  ensure_installed = { "lua", "python", "javascript", "html", "css", "markdown" },
  highlight = { enable = true },
  indent = { enable = true },
})

:TSInstall python installs a grammar, :TSUpdate updates them, :InspectTree shows the tree for the current buffer and :Inspect names the highlight groups under the cursor. A companion plugin, nvim-treesitter-textobjects, adds objects like af and if (a function) that you use as in the text objects chapter. (Plugin setup APIs change between versions: check its README.)

JS Stand-in: why a tree beats a regex

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Fuzzy finding

Telescope (or fzf-lua, snacks.nvim pickers) lists files, buffers, grep results, help tags, diagnostics and LSP symbols in a filter-as-you-type window. It needs ripgrep for live grep and optionally fd. Typical keymaps:

LUA
local t = require("telescope.builtin")
vim.keymap.set("n", "<leader>ff", t.find_files)
vim.keymap.set("n", "<leader>fg", t.live_grep)
vim.keymap.set("n", "<leader>fb", t.buffers)
vim.keymap.set("n", "<leader>fh", t.help_tags)

What to install first, and what not to

A minimal useful stack: a plugin manager, Tree-sitter, LSP with a couple of servers, one fuzzy finder, a Git plugin (see Vim and Git) and a colorscheme. A pre-made distribution (kickstart.nvim is a single readable file; LazyVim and others are bigger) can teach you structure, but copy only what you understand. Each plugin is code you must keep working across upgrades, so prefer fewer. Profile startup with :Lazy profile or nvim --startuptime.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the cmd field in a lazy.nvim spec do?

    cmd, event, ft and keys are lazy-load triggers; build is the post-install step.
  2. 2. In the LSP model, what is Neovim?

    Neovim sends the file and requests to the server process and shows its responses.
  3. 3. Which autocommand event is the right place for LSP-specific keymaps?

    LspAttach runs when a server attaches to a buffer, so buffer-local mappings exist only where they work.
  4. 4. What gives Neovim accurate syntax trees for highlighting?

    Tree-sitter parsers build real syntax trees that update incrementally.

Summary

  • lazy.nvim installs plugins from Lua specs and loads them on triggers; commit lazy-lock.json.
  • The LSP client talks to external language servers; enable them with vim.lsp.enable() and put keymaps in LspAttach.
  • Tree-sitter gives accurate highlighting, folds and text objects.
  • Telescope or a similar picker makes navigation fast.
  • Keep the plugin list short and understand each entry.

Next: Vim and the Terminal