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 rbrings upcounsel-projectile-rg- If point is on the function name, I just hit
M-nto use it as the search term, otherwise I type it in C-c C-onow runsivy-occurand all the candidates end up in a dedicated bufferC-x C-q, bound toivy-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 DiredC-sinvokescounsel-grep-or-swiperand I type in the function nameM-qtakes me toswiper-query-replace, and I just need to enter the new function name, hitRETand thenC-c C-cto confirm my modificationsC-x sfollowed 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.