【发布时间】:2010-01-27 16:31:05
【问题描述】:
我经常在 emacs 中使用命令 grep-find 来搜索我的源文件,但它总是在临时文件和备份文件等中找到匹配项。 grep-find 的默认命令是:
find . -type f -print0 | xargs -0 -e grep -nH -e
我知道我可以在运行它之前对其进行修改以满足我的需求,但是我如何更改它以使其在启动时正确?
【问题讨论】:
我经常在 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 ")
【讨论】:
grep-apply-setting:(grep-apply-setting '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")))
【讨论】:
".min.js" 添加到grep-find-ignored-files,但rgrep 不尊重它。我仍然得到 *.min.js 文件的结果。
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")) 那样对其进行硬连线
如果您使用 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 自己忽略某些文件。然后你得到 命令行中的行为也相同。
【讨论】:
看看当前的 emacs 开发版本是如何处理它的——它提供了大量的排除模式。
【讨论】:
*grep* 缓冲区的顶部(即输出所在的位置),而且非常大。