Emacs中grep-find的默认字符串

8

我经常在Emacs中使用grep-find命令搜索源文件,但是它总是会在临时文件、备份文件等中找到匹配项,这很麻烦。grep-find的默认命令是:

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

我知道在运行之前可以修改它以适应我的需求,但如何更改它以便在启动时就正确无误?

4个回答

9

grep包在前期计算一些默认值(但不一定在包加载时进行)。因此,您需要让它发生,并且然后重新定义查找命令。可以尝试以下操作:

(grep-compute-defaults)
(setq grep-find-command "find . ! -name \"*~\" ! -name \"#*#\" -type f -print0 | xargs -0 -e grep -nH -e ")

4
在Emacs 24中,我不得不使用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

8

如果您使用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 文件的结果。 - mcandre
谢谢这个!对于helm-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")) - Rob

2
如果您使用GNU grep,另一个不错的解决方案是在您的 .bashrc 中添加以下内容。
export GREP_OPTIONS="--exclude=*#* --exclude=*.svn* --exclude=entries --exclude=all-wcprops --exclude=*.xcuserstate --exclude=project.pbxproj --exclude=*.svn-base --exclude=*.tmp"

你可以直接告诉grep忽略特定的文件,这样你也可以在命令行中实现相同的行为。


0

看看目前 Emacs 的开发版本如何处理它--它提供了一个庞大的排除模式列表。


如果列表很大,他们是否会避免在迷你缓冲区中列出完整的命令?我并不真正需要每次搜索时都看到它。 - Zitrax
a) 试一试,看看吧;b) 是的,它们确实避免在迷你缓冲区中列出它。 然而,它会显示在 *grep* 缓冲区的顶部(即输出位置),而且非常巨大。 - offby1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接