6

我在 Emacs 中遇到了 greps 问题。

a) grep 似乎不理解用于搜索 .c 和 .h 文件的 .[ch]。这是 Emacs 使用lgrep命令提供的默认选项。该示例在 .c/.h 文件中搜索单词“global”。

grep -i -nH "global" *.[ch]
grep: *.[ch]: No such file or directory

Grep exited abnormally with code 2 at Mon Feb 16 19:34:36

这种格式无效吗?

b)使用rgrep我得到以下错误:

find . "(" -path "*/CVS" -o -path "*/.svn" -o -path "*/{arch}" -o -path "*/.hg" -o -path "*/_darcs" -o -path "*/.git" -o -path "*/.bzr" ")" -prune -o  -type f "(" -iname "*.[ch]" ")" -print0 | xargs -0 -e grep -i -nH "global"
FIND: Wrong parameter format

Grep finished (matches found) at Mon Feb 16 19:37:10

我在 Windows XP 上使用 Emacs 22.3.1 和 GNU W32 Utils(grep、find、xargs 等)。grep v2.5.3 并找到 v4.2.20。

我错过了什么?

更新:

太糟糕了,不能接受多个答案......因为我的问题的解决方案已经展开。

grep -i -nH "global" *.c *.h

这解决了第一个问题。谢谢luapyad!

(setq find-program "c:\\path\\to\\gnuw32\\find.exe")

emacs 确实在使用 Windows find.exe。强制 gnu32 find 解决了第二个问题。谢谢斯科特弗雷泽。

不过,我还是最喜欢ack

4

6 回答 6

6

我发现使用:

(setq find-program "\"C:/path/to/GnuWin32/bin/find.exe\"")
(setq grep-program "\"C:/path/to/GnuWin32/bin/grep.exe\"")

在窗户上效果更好,因为你可以在路径周围留出空间,最终会搞砸。

请注意,我在.emacs文件中使用了这两个程序。

希望它对其他有需要的程序员有所帮助;)

于 2010-11-13T21:43:34.780 回答
5

对于 a),当前目录中似乎根本没有 .c 或 .h 文件。

对于 b) Windows 正在尝试使用自己的 find 而不是来自 GNU W32 Utils 的 find。尝试:

(setq find-program "c:\\path\\to\\gnuw32\\find.exe")

于 2009-02-17T04:30:21.490 回答
5

好吧,总是有AckAck.el

于 2009-02-17T09:17:06.480 回答
4

亚当·罗森菲尔德的评论值得扩展为答案:

grep -r --include=\*.[ch] --exclude=\*{CVS,.svn,arch} -i -nH

要使此问题中给出的示例起作用,请使用以下命令:

grep -i -nH --include=\*.[ch] "global" *

grep-command将提供默认值的变量设置为M-x grep

(setq grep-command "grep -i -nH --include=\*.[ch] ")

这里还有一些其他有用的 grep 命令行参数:

-n print the line number

-s suppress error messages

-r recursive
于 2009-02-17T23:12:42.820 回答
2

我认为一般问题是 Windows cmd“shell”在文件名扩展正则表达式和通配符方面的行为与 unix shell 非常不同。

要回答您上面的(a),请尝试使用:

grep -i -nH "global" *.c *.h

(如果不存在 *.c 或 *.h,您仍然会收到“无效参数”)。

或者您可以使用命令行选项--include=\*.[ch]使 windows grep 进行“正确”的文件名模式匹配(参见grep --help其他选项)

于 2009-02-17T05:09:34.097 回答
1

如果需要,我通常只M-x grep在提示时使用和更改命令行参数。但我只是试着跑步M-x lgrep,得到了和你一样的东西。它只是意味着*.[ch]当前目录中没有文件匹配。您还可以自定义默认选项以-r递归地包含和搜索子目录:

M-x customize-group RET grep RET

在该缓冲区中搜索 lgrep 以查找/编辑 Grep 模板。

就目前M-x rgrep而言,我怀疑这与find不喜欢默认选项的 Windows 版本有关。该命令在 Linux 上对我来说很好用。在同一个自定义缓冲区中搜索 rgrep 并调整这些选项,直到 Windows 找到满意为止。

抱歉,我无法为 Windows 选项提供更多帮助,但我不熟悉它们。

于 2009-02-17T03:39:26.497 回答