Tuesday, January 27, 2009

Emacs tip: a slightly better find-grep command

I finally got around to tweaking the the find-grep command in emacs to remove one of it's biggest annoyances (to me). Mainly, that it searches .svn directories for content. I'd say at least 99.999999% of the time this isn't what I want.

Here's what I added to my .emacs to fix this:

(setq grep-find-command 
  "find . -type f '!' -wholename '*/.svn/*' -print0 | xargs -0 -e grep -nH -e ")

Ahhh, much better.


  1. Anonymous4:28 PM

    If you use "M-x rgrep" it will automatically construct a find command line which ignores subdirectories of popular version control tools. You may also want to check "M-x lgrep". Both commands take also prefix arguments.

  2. Oooh, thanks for the grep related suggestions!


  3. Anonymous7:30 PM

    You should try out ack. It's vcs aware, and also knows about programming languages.

    There's also an ack-mode for it, but just replacing the find + grep command with ack works just fine.




  4. Anonymous8:18 PM

    Big +1 on the suggestion to use lgrep / rgrep. They’re great.

  5. The other suggestions are quite good, but I thought I'd also point out you could use find's prune capability:

    find . -name .svn -prune -o -type f -print0

    to avoid descending into .svn directories.

  6. Guys -

    Thanks for all the suggestions. lgrep/rgrep will become my new friends.

    Thanks for the reminder about -prune - when I checked the man page, I couldn't find it mentioned there. Should have looked harder...

  7. Anonymous8:58 PM

    Another +1 for rgrep. I use it every day, and just learned that you can further customize the "ignore directories" list (for things like log directories or whatever) by updating the grep-find-ignored-directories attribute. A nice write up of that is here http://mindlev.wordpress.com/2009/09/26/excluding-directories-from-rgrep-in-emacs/



Related Posts with Thumbnails