Skip to content

Recipes

Every recipe here is built from machinery the earlier chapters ran for real; the transcripts named are the evidence. Each names the job, the command, and the check.

Fix the same typo in every file

Job: a word is wrong in several files at once — a renamed product, a misspelled ingredient — and editing file by file is how new typos are born.

Do it: open all the files as arguments, run a substitution in each with argdo (do this in every file on the argument list), write all, and quit (transcript neovim-lab/transcripts/t13-argdo.txt):

nvim --headless batch/cafe-a.txt batch/cafe-b.txt -c "argdo %s/bred/bread/g" -c "wall" -c "qa!"

Check: the lab proved it from outside the editor — grep before found bred in both files, grep after found only bread. Run the grep first in real work, too: the before-check is what tells you the pattern is the word you mean, and %s/old/new/ without the trailing g replaces only the first occurrence on each line.

Edit what a pipe sends you

Job: the output of one program needs a human pass before it goes on — a generated list to prune, a draft to correct.

Do it: the - argument tells Neovim to read its initial buffer from standard input (transcript neovim-lab/transcripts/t11-stdin.txt):

printf "typed by a pipe\nsecond line\n" | nvim --headless - -c "write! stdin-out.txt" -c "qa!"

Interactively — the same command without --headless and the -c parts — this is the classic pipe into the editor, edit, write out move; the buffer starts life as the pipe's text and belongs to no file until you save it under a name, which the headless version demonstrates by writing stdin-out.txt and the lab then reading it back, both lines intact.

Check a file without opening the editor

Job: in a build script or a cron job, ask a question about a file — is its syntax tree the shape I expect, does this parser exist for it — and act on the answer.

Do it: the Tree-sitter inspection of chapter 7 is the template (transcript neovim-lab/transcripts/t06-treesitter.txt): open the file headlessly, luafile a small script that prints the answers, and let the calling shell read them. The same shape serves lint-style checks with a language server attached (chapter 8's script waited for a real answer before exiting), or asking whether a config loads — start headless with the config and print an option; a config that errors announces itself on the error output while one that loads answers the question.

Make git open your editor

Job: commit messages, interactive rebases and merge conflicts should happen in the editor you know.

Do it: this one is documented rather than lab-run, and is a sentence long because both tools are built for it — git config --global core.editor nvim names Neovim as git's editor, and the EDITOR environment variable set to nvim does the same for the long list of other programs that ask. What you get for it is chapter 3's editor, in a buffer whose file is the message or the conflicted file, with :wq to hand it back.