Lesson 7 of 9 · 26m
Configuring Neovim Like an Engineer
A maintainable init.lua: options, leader keys, mappings, autocommands, undo behavior, and the configuration mistakes that make Vim feel haunted.
A Neovim configuration is production software. It has dependencies, startup cost, state, security behavior, and a maintainer — you. Treat it as a small application, not a shrine to copied snippets.
Start with a boring layout
~/.config/nvim/
init.lua
lua/options.lua
lua/keymaps.lua
lua/autocmds.lua
lua/plugins.luainit.lua loads the modules. Set vim.g.mapleader and vim.g.maplocalleader before defining mappings: mappings capture the leader value when they are created, so changing it later does not rewrite existing mappings.
Options should encode intent
vim.opt.number = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.undofile = true
vim.opt.updatetime = 300
vim.opt.splitright = truePersistent undo is one of the highest-value settings in the editor because it preserves undo history across sessions. Do not casually disable swap files either; they are how Vim notices another writer and recovers after a crash. If swap files annoy you, fix the workflow causing multiple writers.
Mappings need modes and descriptions
vim.keymap.set("n", "<leader>w", "<cmd>write<cr>", { desc = "Write buffer" })
vim.keymap.set("n", "<Esc>", "<cmd>nohlsearch<cr>", { desc = "Clear search highlight" })The first argument is the mode: n normal, i insert, v visual, t terminal. Descriptions are not decoration; keymap browsers can surface them months later when you forget your own design.
Autocommands are automation with edges
local group = vim.api.nvim_create_augroup("user_filetypes", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
group = group,
pattern = { "markdown", "gitcommit" },
callback = function()
vim.opt_local.wrap = true; vim.opt_local.spell = true
end,
})Always put autocommands in an augroup with clear = true; otherwise every config reload duplicates them. Use opt_local for buffer-specific behavior. Setting global options in a FileType callback leaks markdown wrapping into your Go files.
Security and provenance
Project-local config through exrc is convenient in trusted projects and dangerous in cloned repositories. Keep it disabled unless you understand your version's trust behavior. Treat copied Lua as code review, not decoration: os.execute, network calls, and opaque blobs do not belong in your editor.
Field notes
- Run
:checkhealthbefore debugging plugins; clipboard, terminal, treesitter, and provider failures are usually diagnosed there. - Keep your config in git and pin plugin versions. “My editor changed overnight” is an avoidable outage.
Was this lesson helpful?