8

I often use the command grep-find in emacs to search through my source files, but it's annying that it always finds matches in temporary files and backup files and so on. The default command for grep-find is:

find . -type f -print0 | xargs -0 -e grep -nH -e

I know I can modify it before I run it to match my needs but how do I change it such that it's correct on startup ?

Zitrax
  • 19,036
  • 20
  • 88
  • 110

4 Answers4

9

The grep package computes a bunch of defaults up front (but not necessarily on package load). So you'll want to get that to happen, and then redefine the find command. Something like:

(grep-compute-defaults)
(setq grep-find-command "find . ! -name \"*~\" ! -name \"#*#\" -type f -print0 | xargs -0 -e grep -nH -e ")
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 4
    I had to use `grep-apply-setting` in Emacs 24: `(grep-apply-setting 'grep-find-command "find . ! -name \"*~\" ! -name \"#*#\" -type f -print0 | xargs -0 -e grep -nH -e ")` – sandinmyjoints Nov 16 '12 at 16:01
8

If you use lgrep or rgrep instead of grep-find, you can set up ignored files/dirs in advance:

(eval-after-load "grep"
  '(progn
    (add-to-list 'grep-find-ignored-files "*.tmp")
    (add-to-list 'grep-find-ignored-directories "_darcs")))
mcandre
  • 22,868
  • 20
  • 88
  • 147
sanityinc
  • 15,002
  • 2
  • 49
  • 43
  • I added `".min.js"` to `grep-find-ignored-files`, but rgrep doesn't respect it. I still get results with *.min.js files. – mcandre Apr 24 '13 at 16:22
  • Thanks for this! Also useful for `helm-ag`, which also uses the settings of the `grep-find-ignored*` variables. Instead of adding to the existing list, I've chosen to hardwire it like so `(customize-set-variable 'grep-find-ignored-directories (list "SCCS" "RCS" "CVS" "MCVS" ".svn" ".git" ".hg" ".bzr" "_MTN" "_darcs" "{arch}" "objects" "build" "bin" "out" "lib"))` – Rob Mar 13 '15 at 09:09
2

If you use GNU grep another nice solution is to put something like this in your .bashrc

export GREP_OPTIONS="--exclude=*#* --exclude=*.svn* --exclude=entries --exclude=all-wcprops --exclude=*.xcuserstate --exclude=project.pbxproj --exclude=*.svn-base --exclude=*.tmp"

and just tell grep itself to ignore certain files. Then you get the same behavior from the command line too.

JD Brennan
  • 992
  • 1
  • 10
  • 20
0

Take a look at how the current development version of emacs handles it -- it supplies a huge list of exclusion patterns.

offby1
  • 6,767
  • 30
  • 45
  • If the list is huge do they avoid listing the full command in the mini buffer ? I don't really need to see it each time I do a search. – Zitrax Jan 27 '10 at 18:17
  • a) try it and see; b) yes, they do avoid listing it in the minibuffer. It does, however, show up at the top of the `*grep*` buffer (i.e., where the output goes), and it's pretty huge. – offby1 Jan 31 '10 at 23:19