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:


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

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

(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 in ~/bin, which is in my PATH. And I’ve added alias 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()


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

  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.)

  3. Perfect example for xargs….


    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 *