Bulk multi-line find and replacement with Dired instead of SED

Table of Contents

Scenario: I have a directory full of code files (in this case, 51 of them) that each need a find-and-replace executed; however, it is adding a line to part of the code, so replace one line with several. This means that SED cannot do the trick; you’ll get errors like,

> > > sed: -e expression #1, char 49: unterminated `s' command

Rather than spending the time figuring out how to encode things for SED, the simplest solution was just to use emacs inimitable dired. It went like this:

  1. Open dired into the directory with your files C-x C-d or M-x dired
  2. Select all files t
  3. Q or M-x dired-do-query-replace-regexp
  4. Your query
  5. It will ask you to okay the change; you can prevent it from asking this for the rest of the fixes in ALL files with Y
  6. Now you have a bunch of altered buffers open. You can save them all with C-x s

It would have been trickier if they were in subdirectories, but with them all horizontal it worked quickly and without issue.

Tory Anderson avatar
Tory Anderson
Web App Engineer, Digital Humanist, Researcher, Computer Psychologist