Default string for grep-find in emacs
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 ?
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 ")
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")))
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.
Take a look at how the current development version of emacs handles it -- it supplies a huge list of exclusion patterns.
精彩评论