Skip to content

Configuring with Lua

Neovim reads one file at startup: init.lua, in the config folder chapter 2 located (~/.config/nvim/ on an ordinary Linux machine). Whatever the file does — and it is an ordinary Lua program, run top to bottom — is your configuration. This chapter sets options, a leader key and a mapping in that file, and then does the thing that separates this guide from most config tutorials: it asks the editor what values it is holding, and prints the answers as proof.

The three verbs of configuration

Nearly everything in an init.lua is one of three shapes:

  • vim.opt.name = value — set an option: line numbers, tab width, search behaviour. Options are the hundreds of switches Vim always had; vim.opt is how Lua holds them.
  • vim.g.name = value — set a global variable: values read by the editor itself or by plugins. The leader key below is one.
  • vim.keymap.set(mode, keys, action) — a mapping: when these keys are pressed in this mode, do this instead.

The lab's init.lua, whole

This is the file the lab ran (kept at neovim-lab/config/init.lua, transcript neovim-lab/transcripts/t03-init-lua.txt). It is deliberately small — five options, one leader, one mapping, and one autocmd that chapter 5 will put to work:

-- The lab's init.lua: small on purpose, every line proven in the transcripts.
vim.g.mapleader = ' '            -- the leader key is Space

vim.opt.number = true            -- show line numbers
vim.opt.tabstop = 4              -- a tab displays as 4 columns
vim.opt.expandtab = true         -- Tab key inserts spaces
vim.opt.ignorecase = true        -- searches ignore case...
vim.opt.smartcase = true         -- ...unless the pattern has a capital

-- Press leader-w to save the file.
vim.keymap.set('n', '<leader>w', '<cmd>write<CR>', { desc = 'Save file' })

-- After any yank (copy), append a line to a log file: proof an autocmd fired.
vim.api.nvim_create_autocmd('TextYankPost', {
  desc = 'Log every yank to yank-log.txt',
  callback = function()
    local f = assert(io.open(vim.fn.expand('~/yank-log.txt'), 'a'))
    f:write('yanked: ' .. vim.fn.getreg('"') .. '\n')
    f:close()
  end,
})

Three lines deserve a slow read. The leader key is a prefix you choose for your own mappings, so they cannot collide with the editor's — here it is Space, set in a global that must be assigned before any mapping uses <leader>, because the mapping remembers the leader in force when it was defined. ignorecase with smartcase is the search setting most people actually want: searches are case-insensitive until you type a capital letter, which is taken as a sign you mean it. And expandtab with tabstop is a standing argument in the editor world — spaces or tabs — which this guide takes no side in; it shows the setting because nearly everyone changes it first.

Asking the editor

The lab started Neovim headlessly with this config in place and asked for the values back. The answers, from the transcript:

tabstop = 4
expandtab = true
ignorecase = true
number = true
leader = " "
map <leader>w = "<Cmd>write<CR>"

Every line of the file is visible in that output — the options hold what the file set, the leader is Space, and <leader>w is bound to the command the file named. At your own keyboard the same questions are interactive: :set tabstop? prints an option's current value, and :verbose map <leader>w shows a mapping and the file and line that defined it — the single most useful diagnostic when a configuration grows, because a big init.lua is edited by past-you, and past-you made choices present-you has forgotten.

The older door still opens

Vim users arrive with an init.vim — the same startup file in Vimscript. Neovim still reads that name if no init.lua exists, and a Lua config can call Vimscript and be called by it, so nothing about migrating is a cliff edge: move one setting at a time, asking the editor after each move. The common failure is having both files present and assuming both run — only init.lua does, when both exist. And a config file that errors does not stop the editor starting: it starts with defaults and shows the error, which is chapter 12's first case study.