我经常在 emacs 中使用命令 grep-find 来搜索我的源文件,但它总是在临时文件和备份文件等中找到匹配项。grep-find 的默认命令是:
find . -type f -print0 | xargs -0 -e grep -nH -e
我知道我可以在运行它之前对其进行修改以满足我的需要,但是如何更改它以使其在启动时正确?
该grep
包预先计算了一堆默认值(但不一定在包加载时)。所以你会想要让它发生,然后重新定义 find 命令。就像是:
(grep-compute-defaults)
(setq grep-find-command "find . ! -name \"*~\" ! -name \"#*#\" -type f -print0 | xargs -0 -e grep -nH -e ")
如果您使用 lgrep 或 rgrep 而不是 grep-find,您可以提前设置忽略的文件/目录:
(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.
看看当前的 emacs 开发版本是如何处理它的——它提供了大量的排除模式。