batching ex with sed

By Paul Murphy, author of The Unix Guide to Defenestration

It can be hard to remember that vi is the obsessively user friendly interface to ex, the user friendly version of ed, but sometimes that relationship can be important to you - especially for repetitive tasks.

sed, the stream editor, is a batch oriented ex. You can define and save macros (in .exrc) in vi but I've always found this to be a major pain because they're are hard to change and even harder to keep track of.

Calling sed from within vi is both much easier and more flexible since the erzatz macros you create can be stored and edited externally - just remember to make a backup copy of your working file:

:w %.backup

before starting!

With your backup in place, something like:

:w filename
!!sed -f my_sed_file %

will first write out the file, then empty the on-screen copy, and finally bring everything back with the ex commands in my_sed_file applied to it.