Any programmer knows the importance of refactoring. It’s even hard to believe a programmer can live without it. There are plenty of use cases for refactoring, but the following happens often enough that I am glad Emacs sits next to me every day.

Imagine you are working on a medium-to-big size project. You have functions everywhere, maybe constants here and there to make your life easier. Functional programmers have no time for variables, so I won’t go down that road.

At one point you discover a function that could use a different name. Clarity, readability, or just mere kindness towards your fellow co-workers prompts you to come up with something better. Naturally, the function is used in many places around the codebase, so it’s time to bring out the heavy guns.

There are plenty of ways to solve this problem, especially in Emacs, but this is how I approach the situation:

  • C-c p s r brings up counsel-projectile-rg
  • If point is on the function name, I just hit M-n to use it as the search term, otherwise I type it in
  • C-c C-o now runs ivy-occur and all the candidates end up in a dedicated buffer
  • C-x C-q , bound to ivy-wgrep-change-to-wgrep-mode, makes the new buffer editable. It’s easy to remember because it’s the same key binding to enable Wdired from Dired
  • C-s invokes counsel-grep-or-swiper and I type in the function name
  • M-q takes me to swiper-query-replace, and I just need to enter the new function name, hit RET and then C-c C-c to confirm my modifications
  • C-x s followed by ! is all it takes now to save the files involved in my refactoring

If it looks like a lot to remember, well, it is. Only the first times, though. After a while it becomes second nature, and editing an occurrence in multiple files feels like a gentle breeze down your neck.

Projectile and Ivy/Counsel/Swiper are really invaluable tools.