softwhere: an alternative to grep for searching your source trees

At a previous workplace, we had a little script built around glimpse that indexed our source tree, making it quick to search for keywords. Someone cleverly called this script softwhere, and gave it a handy alias of sw.

When I started working on Mozilla, I wanted softwhere. And I thought to myself: “Hey, I run OS X now. Isn’t every file on my computer automatically indexed?” The answer, of course, is yes. And thus, softwhere 2.0 was born:

#!/bin/sh

files=`mdfind -onlyin /src/mozilla-central "$*"`

for file in $files; do
    grep -H "$*" $file
done

(You’ll need to replace /src/mozilla-central in the mdfind command above to wherever you store your primary source code repository.)

I’ve saved softwhere.sh in ~/bin, which is in my PATH. And I’ve added alias sw=softwhere.sh into my .bashrc. And thus:

joe@joe-laptop:~$ sw imgLoader::Init
/src/mozilla-central/modules/libpr0n/src/imgLoader.cpp.orig:nsresult imgLoader::InitCache()
/src/mozilla-central/modules/libpr0n/src/imgLoader.cpp.orig:nsresult imgLoader::Init()
joe@joe-laptop:~$

5 comments

  1. Neat, I’ve played with similar things, but not using the mac index; very cool!

    http://vocamus.net/dave/?p=138

  2. And here’s a wrapper I’ve written integrating grep into Vim. (grep’ing the whole source tree takes less than a second on my Linux box.)

    http://jlebar.com/2011/3/23/Vim_search_plugin.html

  3. Perfect example for xargs….

    #!/bin/sh

    mdfind -onlyin /src/mozilla-central “$*” | \
    xargs grep -H “$*”

  4. +1 for Ack! I find it even better than using Spotlight, even though it does not use an index.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>