Batch Editing Files with Vim

Waaay back in 2018, Julia Evans wrote this terrific post about how to edit a BUNCH of files by writing a little ed script. You should absolutely go read the whole post where she explains how to use ed to perform a sequence of edits she’d normally do in Vim.

I just want to add the note that the code she ends up with is also perfectly valid Vimscript. So you can run the EXACT same script in Vim!

Vim offers a plethora of ways to run scripts non-interactively, but the cleanest in this scenario is to use its sexytime silent mode:

vim -es < my-script.ed file-to-edit.txt

(If you are a cat fancier like Evans, cat my-script.ed | vim -es file-to-edit.txt works just as well.)

If her post has PIQUED your interest in the standard text editor, I can heartily endorse reading this great explainer1 — when I stumbled on it after many years of instinctively shying away from ed, all I could think was how much I would have killed for this kind of user-friendly POWER back when I was tapping out BASIC in the early 90s.

  1. And then following ed(1) Conference on Mastodon and Twitter↩︎