While working on a huge code-base with several thousand source files, it becomes difficult to remember where each file is. If you use conventions like one file per class, you can at least figure out the file name. E.g. the definition of class Foo
would be found in Foo.class.xx
or something like that.
Thankfully, ack makes it easy to find the location files in a project. Just say, ack -g Foo.class
and voila! it tells you to dig in modules/frob/model/include/Foo.class.xx
or whatever abominable directory hierarchy it may be embedded in. So when you decide to kill that error at line 324 of class Foo, all you need to do is:
vim `ack -g Foo.class` +324
Unfortunately, typing this out is a bit tedious as well, so we bring in a little shell script (I call it vis
, for vi m on s earched filename):
#!/bin/bash
# Search and edit files. Use it like:
# $ vis Foo.bar [...]
if [ $# -lt 1 ]; then
exit 255;
fi
fname=$1;
shift;
vim `ack -g \\\b${fname}[^\/]*\$ | head -1` $@
This will invoke vim
with the first filename that ack returns for the filename pattern (yes, it can be a Perl regex), and you can pass all other vim
arguments as usual after the file name. Nifty, eh? The regex above will match file names starting with the given search pattern (with some caveats).
PS: It’s not too hard to make the script choose the editor dynamically, but people usually love to use one editor consistently. I use both Emacs and Vim, so I’m planning to make this script take the editor as an argument and have two convenient aliases, one each for vim
and emacsclient
which will handle passing the editor as an argument.