【问题标题】:lgrep and rgrep in EmacsEmacs 中的 lgrep 和 rgrep
【发布时间】:2010-10-08 00:13:45
【问题描述】:

我在使用 Emacs 中的 grep 时遇到问题。

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 上使用带有 GNU W32 实用程序(grep、find、xargs 等)的 Emacs 22.3.1。 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

【问题讨论】:

  • 一个更简单的使用rgrep的方法是:grep -r --include=*.[ch] --exclude=*/{CVS,.svn,arch,.hg,_darcs,.git, .bzr} -i -nH 全局 .
  • 您能否更改您的帖子以准确反映 find-program 的答案?你今天给我带来了一些困难。不同之处在于需要双反斜杠。 (setq find-program "C:\\path\\to\\gnuw32\\find.exe")
  • @Matthew Talbert,我认为降价处理器正在吞噬额外的反斜杠。我提交了一个修改来解决这个问题。

标签: emacs grep


【解决方案1】:

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

M-x customize-group RET grep RET

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 这帮助了我今天。谢谢!
    【解决方案3】:

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

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

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

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

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

    【讨论】:

      【解决方案4】:

      嗯,总是有AckAck.el

      【讨论】:

      • 噢!我完全忘记了阿克!在 Vim 中喜欢它。谢谢!
      【解决方案5】:

      Adam Rosenfield 的评论值得扩展为答案:

      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
      

      【讨论】:

        【解决方案6】:

        我发现使用:

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

        在 Windows 中效果更好,因为您可以在路径周围留出空间并且最终会搞砸。

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多