It’s not often that many of my favorite tools intersect in an exciting way such that I find myself being saved tons of time, but it happened today and I thought I would share a powerful editing technique. The tools used were vim and ack. The payoff was to make 250 changes across dozens of files with three simple command-line operations.
Today the Ember team announced the 1.0-pre release of this product and I was anxious to implement it with my work codebase.
After slotting the new version, on initial
boot up of my app in Chrome reported 245 errors. My code base, apparently, had 220 uses of the
getPath() method. As of 1.0-pre, this method is deprecated and warnings are bubbled from Ember into Chrome. The fix is simple, change from using the verbose
getPath() and use the more generic
get() method. So the task was to change all occurrences in all places across some 3 dozen files from
There are many ways to fix this, but since I just finished Drew Neil’s
excellent Practical Vim, I
knew of a way to tear through this problem using Vim’s
- The first task was to find all the files that have
getPathin them. The Ack tool is great at this:
- Copy that command to the clipboard (I did it using
tmux, a great utility that I learned about thanks to Brian Hogan’s book on the topic)
- Enter Vim
- This loaded up all those matches. In the first file that matched, I
performed the following:
qa:%s/getPath/get/gq. This recorded a macro called
athat I could run to change
get. It also made this change in the current file I was editing
uto undo the change I just effected upon the first buffer since I was about to run the command on a population of which this file was a member.
:argdo normal @a
- Watch in awe as Vim ran through the argument list and executed the
amacro’s action, a global substitution of
:wallto save all the changes
- Done and commit the work
Talk about a powerful set of tools there! With
ack, I tore
through 245 results in a matter of seconds. I hit reload in Chrome and I had
8 other, unrelated, warnings.
If your editor can’t batch regex replacements and allow you to beat files senseless like this, you might need to consider a change.
UPDATE: Via Twitter, Drew commented that
:argdo %s/getPath/get/g would also work. Even fewer keystrokes!