我经常在Emacs中使用grep-find命令搜索源文件,但是它总是会在临时文件、备份文件等中找到匹配项,这很麻烦。grep-find的默认命令是:
find . -type f -print0 | xargs -0 -e grep -nH -e
我知道在运行之前可以修改它以适应我的需求,但如何更改它以便在启动时就正确无误?
grep
包在前期计算一些默认值(但不一定在包加载时进行)。因此,您需要让它发生,并且然后重新定义查找命令。可以尝试以下操作:
(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")))
grep-find-ignored-files
中添加了 ".min.js"
,但是 rgrep 没有遵守它。我仍然会得到带有 *.min.js 文件的结果。 - mcandrehelm-ag
也很有用,它也使用了grep-find-ignored*
变量的设置。我选择硬编码而不是添加到现有列表中,像这样(customize-set-variable 'grep-find-ignored-directories (list "SCCS" "RCS" "CVS" "MCVS" ".svn" ".git" ".hg" ".bzr" "_MTN" "_darcs" "{arch}" "objects" "build" "bin" "out" "lib"))
。 - Robexport GREP_OPTIONS="--exclude=*#* --exclude=*.svn* --exclude=entries --exclude=all-wcprops --exclude=*.xcuserstate --exclude=project.pbxproj --exclude=*.svn-base --exclude=*.tmp"
你可以直接告诉grep忽略特定的文件,这样你也可以在命令行中实现相同的行为。
看看目前 Emacs 的开发版本如何处理它--它提供了一个庞大的排除模式列表。
*grep*
缓冲区的顶部(即输出位置),而且非常巨大。 - offby1
grep-apply-setting
命令:(grep-apply-setting 'grep-find-command "find . ! -name \"*~\" ! -name \"#*#\" -type f -print0 | xargs -0 -e grep -nH -e ")
。该命令的作用是设置grep-find-command
变量,使其包含指定的字符串,以便在执行grep
搜索时使用。 - sandinmyjoints