Refactoring Multiple Files

When I get tired of blogging I’m going to write some Elisp that everyday will make a post that says, “Abo-abo has a great post today. Go read it.” Really, if you don’t already have (or emacs in your feed, you should add it immediately. I learn something new from it almost every day.

One of his latest posts is about his refactoring workflow. The problem is to change a function name in every file in a directory. There are multiple subdirectories. How would you do this? I can think of several ways, mostly including dired and perhaps keyboard macros. Abo-abo has another approach. His steps are

  1. Call rgrep to get a list of every occurrence of the function name.
  2. Call wgrep so that you can edit the rgrep output and have the results reflected in the original files.
  3. Call iedit to change every instance of the function name at once.
  4. Quit iedit.
  5. Quit wgrep, writing the changes back to the files.

See abo-abo’s post for the details.

This is a really outstanding post and I encourage everyone to take a look. I hadn’t been using iedit or wgrep but installed them so that I could take advantage of abo-abo’s technique.

I’ve long been a multple-cursors user so I was interested in abo-abo’s use cases for iedit versus multiple-cursors and queried him on it. You can read his answer here. One thing that struck me is that he described iedit as “a drop-in `occur` and `query-and-replace`.” You can see how that works with his refactoring process. Most excellent.

This entry was posted in General and tagged . Bookmark the permalink.