My Neovim config is well over 4,000 lines of Lua. The lockfile lists 60 plugins. The README in my dotfiles says 35, which tells you how often I update the README.

Some of it I like and some of it is a mess. One day in September I rebuilt most of it, measured every step, and then reverted the lot the following afternoon.

The shape of it

Plugins are managed by lazy.nvim. The leader key is ;. The entry point loads things in a fixed order: options, keymaps, autocommands, filetypes, three modules of my own, and then the plugins.

Language servers use the native API that arrived in Neovim 0.11. I don't configure anything through nvim-lspconfig. Each server gets a table and a call to vim.lsp.enable, and only if its binary is really there:

-- Configure C LSP (clangd). Prefer the system clangd: on macOS it knows
-- where the SDK headers live, which Mason's build does not.
local clangd_bin = vim.fn.exepath("clangd")
if clangd_bin == "" then
    clangd_bin = vim.fn.stdpath("data") .. "/mason/bin/clangd"
end
if vim.fn.executable(clangd_bin) == 1 then
    vim.lsp.config.clangd = {
        cmd = { clangd_bin },
        filetypes = { "c" },
        root_markers = { ".clangd", "compile_commands.json", "compile_flags.txt", "Makefile", ".git" },
        capabilities = capabilities,
        init_options = {
            fallbackFlags = { "-xc", "-Wall", "-Wextra", "-std=c11" },
        },
    }
    vim.lsp.enable("clangd")
end

The -xc flag is there because clangd was parsing lone .h files as Objective-C++ and then rejecting -std=c11. I only found that out by opening a header and getting a screen of errors.

For files I use Oil, which shows a directory as a buffer you can edit: rename a file by changing its line, delete it by deleting the line. Harpoon holds the four files I'm working in on Shift+h/j/k/l.

C# in Neovim

I write C# for a living, so this is the part of the config that matters most to me. I use easy-dotnet.nvim, which runs Roslyn and adds commands for building, testing, running and managing packages. My older roslyn.lua is still in the repo with every line commented out.

Everything .NET lives under ;n: ;nb builds, ;nt runs tests, ;nd starts the debugger, and ;nem adds an EF migration.

The piece I wrote myself is go-to-definition. When Roslyn jumps into framework code, it shows you decompiled source. That's readable, but it has no comments and none of the original structure. I have a clone of the dotnet/runtime repository on disk, so my gd looks there first:

-- Custom go to definition for C#: prefer local runtime source, fallback to LSP
local function smart_goto_definition()
    local ft = vim.bo[ev.buf].filetype

    -- For C# files, first try to find in local .NET Runtime source
    if ft == "cs" and goto_dotnet_runtime_source() then
        return
    end

    -- Fallback to standard LSP definition (which includes decompilation)
    vim.lsp.buf.definition()
end

The lookup itself is crude. It takes the word under the cursor and runs ripgrep for a matching class, struct, interface, enum or record declaration, first in System.Private.CoreLib and then in all of src/libraries. It opens the first hit. It gets overloads and nested types wrong, and I still prefer it, because when it's right I'm reading the real Dictionary<TKey, TValue> with the authors' comments.

A build key that works everywhere

The two modules I'd keep if I lost everything else are a build detector and a quickfix runner. Together they give me one key, ;bb, that builds whatever project I'm in.

The detector walks up from the current file and stops at the first thing it recognises:

for _, dir in ipairs(directories) do
    if vim.fn.filereadable(dir .. "/Makefile") == 1 or vim.fn.filereadable(dir .. "/makefile") == 1 then
        return "make", dir
    end
    if vim.fn.filereadable(dir .. "/build.zig") == 1 then
        return "zig build", dir
    end
    if vim.fn.filereadable(dir .. "/Cargo.toml") == 1 then
        return "cargo build", dir
    end

    local solutions = vim.fn.glob(dir .. "/*.{sln,slnx}", false, true)
    if #solutions > 0 then
        return "dotnet build " .. vim.fn.shellescape(solutions[1]), dir
    end
    -- ...then *.csproj, then package.json with a build script
end

The runner starts the command as a job and streams its output into the quickfix list a line at a time. The error format covers gcc and clang, the sanitizers, and MSBuild's file(line,col): error style, so Ctrl+n jumps to the next compiler error whether the project is C or C#.

Streaming has the usual trap. Output arrives in chunks, and a chunk can end in the middle of a line, so the runner holds the last piece back until the rest shows up:

local tails = { stdout = "", stderr = "" }
local function consume(stream, data)
    if not data or #data == 0 then
        return
    end

    data[1] = tails[stream] .. data[1]
    tails[stream] = table.remove(data)
    append_lines(list_id, data, cwd)
end

I wrote the same fix in Emacs Lisp for a different tool a month later. Apparently I need to learn that one twice.

The parts that are a mess

I have three fuzzy finders installed. fzf-lua handles files and grep. Snacks handles recent files, projects, Git and about twenty other lists. Telescope is still there because two plugins depend on it, and its config file is leftover LazyVim code that refers to a global variable which doesn't exist in my setup. The Snacks config still has this in it:

-- { "<leader>fb", function() Snacks.picker.buffers() end, desc = "Buffers" }, -- Conflict with fzf-lua

There's more. Mason is declared in two files with two different tool lists. The auto-pairs plugin has an empty config function, so its setup never runs and none of my tuning for it applies. Five colour schemes load at startup and I use one. undodir is set in two places, and for a while Neovim was writing undo history to an empty directory while 39 real undo files sat unused in another.

I know about every one of these, because in September I fixed them all.

One afternoon in September

On September 18 I sat down to clean the config up properly. Neovim 0.12 had made a lot of plugins unnecessary, so I replaced nvim-cmp and its sources with native completion, dropped toggleterm for Snacks' terminal, and went from three pickers to one. That took the plugin count from 57 to 38.

Then I started measuring. Startup went from 56ms to 47ms with the restructure, and to about 24ms once I stopped loading things eagerly. The biggest single item was easy-dotnet, which cost 32ms on every start, including when I was opening a Lua file.

Startup had never bothered me, though. The pause when I opened the first C# file of a session did, so I measured that:

first .cs open        209ms
second .cs open         6ms

Treesitter was my first suspect, and it wasn't responsible. Parsing a 419-line C# file took 0.4ms. The time went here:

LSP client handshake (Roslyn attach)   ~185ms
easy-dotnet + plenary/dap/dapui load    ~45ms
treesitter                               0.4ms

All of that was one-time setup, and lazy-loading had scheduled it for the exact moment I asked to look at code. The fix was to do it earlier, while I wasn't watching. After startup, on a 200ms idle timer, the config loaded the plugin and created a throwaway buffer with the right filetype, which made the language server attach:

local function warm_filetype(ft)
    local buf = vim.api.nvim_create_buf(false, true) -- unlisted, scratch
    if buf == 0 then
        return
    end

    -- A real path matters: easy-dotnet resolves the solution from the
    -- buffer name, and vim.lsp root detection needs something on disk to
    -- walk up from.
    pcall(vim.api.nvim_buf_set_name, buf, vim.fn.getcwd() .. "/.dev1se-warmup." .. ft)
    pcall(vim.api.nvim_set_option_value, "filetype", ft, { buf = buf })

    -- Give the attach a moment to fire, then drop the buffer. The client it
    -- started is owned by the LSP layer and outlives the buffer.
    vim.defer_fn(function()
        if vim.api.nvim_buf_is_valid(buf) then
            pcall(vim.api.nvim_buf_delete, buf, { force = true })
        end
    end, 1000)
end

The warmup only ran when the working directory had a .sln or .csproj in it, so a Zig project never started Roslyn. Afterwards the first C# file opened in 28ms and the first TSX file in 55ms, down from 158ms.

I also learned something about lazy.nvim that I hadn't expected. Removing nvim-dap from easy-dotnet's dependency list didn't stop the debugger from loading on every C# file. The plugin did a pcall(require, "dap") to see whether it was available, and that require alone was enough for lazy.nvim to load dap, its UI and a third library.

The fix I undid in seven minutes

Not all of it worked. Opening a C# file also had a visual flicker, which I timed: Roslyn attached at 700ms and repainted every identifier, style suggestions appeared at 2 seconds, and one of them vanished again at 5. I fixed most of that by delaying semantic tokens and hiding hint-level diagnostics for C#.

Then I got greedy and made Roslyn start as soon as I browsed into a .NET directory in Oil. It seemed clever. Roslyn promptly initialised against whichever folder I happened to be looking at, opening a C# file started throwing errors, and Oil itself got slower. The commit that added it is stamped 16:12. The commit that removed it is stamped 16:19.

And then I reverted everything

The next afternoon I restored the Neovim directory to exactly what it had been before any of this. The revert touched 60 files.

The commit message says what I did and not why. Here is what that day's diff contained. A dozen of my mappings had new names. Completion worked differently, and for a few hours neither Tab nor Enter accepted a suggestion until I noticed and fixed it. The pickers had moved from the middle of the screen to the bottom. One of the commit messages from that day, restoring a comment mapping I'd removed in favour of the built-in one, says: "Native gc/gcc work, but the fingers know ;/."

So the config I use today is the old one, with three pickers, an auto-pairs plugin that does nothing, and a 200ms wait on the first C# file of the day. Every fix I described above exists only in the Git history.

I don't think the afternoon was wasted. I know exactly where the time goes now, and I have the measurements. I've also started a second config next to the main one, built on Neovim's own vim.pack in place of lazy.nvim, and a small shell function lets me pick which config to launch:

function nvims() {
  items=("default" "minimal" "lazyvim" "nvim-dev")
  config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config:" --height=~50% --layout=reverse --border --exit-0)
  if [[ -z $config ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $config == "default" ]]; then
    config=""
  fi
  NVIM_APPNAME=$config nvim $@
}

NVIM_APPNAME makes Neovim read its config from a different directory, so the experiment can't break the editor I work in. I should have done the September overhaul this way.

What I'd tell someone starting

Measure before you optimise, and measure the thing that annoys you. I spent time on startup when my problem was the first file open, and those turned out to have different causes.

Check that your config does what it says. Mine had a plugin whose setup never ran and a formatter that was configured for no filetypes, and both had been like that for months.

And change one thing at a time in the editor you rely on. A big rewrite belongs in a second config that you can try for a week and walk away from.