Bulk multi-line find and replacement with Dired instead of SED
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:
- Open dired into the directory with your files
C-x C-d
orM-x dired
- Select all files
t
Q
orM-x dired-do-query-replace-regexp
- Your query
- 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
- 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.