Skip to content

Lua Automation

Chapter 4 configured the editor. This chapter makes it react: running code when things happen, adding commands of your own, and driving the whole editor from Lua scripts — the habit that turns a configured editor into a tool fitted to one person's work. Everything shown was run in the lab; the transcripts named are the receipts.

The vim table is the editor

From Lua, the editor is one global table named vim, organised by job:

Handle What it reaches
vim.opt, vim.g Options and globals (chapter 4)
vim.keymap Mappings
vim.api The editor's API proper: buffers, windows, commands, events — functions prefixed vim.api.nvim_
vim.fn Every Vimscript function, callable from Lua (vim.fn.getreg, vim.fn.expand — both appear in chapter 4's config)
vim.cmd Run an Ex command from Lua, as a string

That is the entire mental model. If Neovim can do it, some entry in that table does it, and :help vim is the map.

Autocmds: the editor reacts

An autocmd (autocommand) is a promise: when this event happens, run this code. The events cover the editor's whole life — a file is opened (BufReadPost), about to be saved (BufWritePre), text is yanked (TextYankPost), a language server attaches (LspAttach, chapter 8), and dozens more (:help autocmd-events lists them all).

The lab's proof is the autocmd already sitting in chapter 4's init.lua: on every yank, append the yanked text to a log file. The lab then opened a file, yanked its first line with the keystrokes ggyy, and quit. The log file, which had not existed before (transcript neovim-lab/transcripts/t04-autocmd.txt):

$ cat ~/yank-log.txt
yanked: Shopping list

A toy, deliberately — but the shape is the one real configs use constantly. Format a file every time it is saved: a BufWritePre autocmd. Restore the cursor to where it was when a file is reopened: BufReadPost. Highlight text for a moment after yanking it — a setting so popular it is the example in Neovim's own help — is TextYankPost again. Each is the same three lines: the event, a pattern for which files it applies to, and a callback.

Your own commands

vim.api.nvim_create_user_command adds an Ex command — a :Word of your own. Chapter 6's hand-written plugin is built from one: :LabGreet asks for a name and stamps a greeting at the top of the buffer, run for real in transcript neovim-lab/transcripts/t09b-pack-plugin.txt. In your init.lua the same call makes personal commands — :Trim to strip trailing whitespace, :Notes to open your notes file — without the plugin packaging.

The ways of running Lua

  • :lua ... — one line, typed at the editor, for questions and quick experiments (:lua print(vim.opt.tabstop:get()) is chapter 4's asking, interactively).
  • :luafile file.lua — run a whole file of Lua in the current session; most lab transcripts were driven this way.
  • nvim -l script.lua — run a Lua script and exit, with the editor never opening a screen: Lua with the whole vim API as a scripting environment, for automation outside editing altogether.
  • Headless -c chains — chapter 1's taste: editor commands strung on one shell line.

One honest lesson from the lab's own scripting, recorded because it cost a real failure: when a script feeds keystrokes (vim.api.nvim_feedkeys), feed the whole sequence in one call. The lab's first attempt at chapter 3's edit fed the keystrokes in separate calls — search, then change, then the replacement text — and the characters went astray mid-edit, mangling the line it produced. Fed as one sequence, the identical keystrokes produced exactly the intended line. The editor processes a fed batch as one piece of typing; splitting a single thought across batches splits it in places a typist never would.