代码之家  ›  专栏  ›  技术社区  ›  cschol

Emacs中的lgrep和rgrep

  •  6
  • cschol  · 技术社区  · 15 年前

    我在Emacs的greps有问题。

    a)grep似乎不理解用于搜索.c和.h文件的[c h]。这是Emacs提供的默认选项, 勒格雷普 命令。示例是在.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)使用 RRGRP 我得到以下错误:

    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
    

    我在WindowsXP上使用Emacs22.3.1和GNUW32实用程序(grep、find、xargs等)。grep 2.5.3版,找到v4.2.20版。

    我错过了什么?

    更新:

    太糟糕了,一个人不能接受多个答案……因为我的问题的解决方案是分散的。

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

    这就解决了第一个问题。谢谢卢帕耶德!

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

    Emacs确实在使用windows find.exe。强制GNU32解决第二个问题。谢谢Scottfrazer。

    不过,我还是喜欢 ack 最好的。

    6 回复  |  直到 9 年前
        1
  •  5
  •   elmarco    15 年前

    嗯,总有 Ack Ack.el

        2
  •  6
  •   oscarcardoso    14 年前

    我发现使用:

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

    在Windows中工作得更好,因为您可以在路径周围放置一个空间,最终会出错。

    注意,我在我的 .emacs 文件。

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

        3
  •  5
  •   scottfrazer    15 年前

    对于a)看起来当前目录中没有.c或.h文件。

    对于b)Windows试图使用自己的find,而不是GNUW32实用程序中的find。尝试:

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

        4
  •  3
  •   Justin Tanner    10 年前

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

    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
    
        5
  •  2
  •   luapyad    15 年前

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

    要回答上述(a),请尝试使用:

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

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

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

        6
  •  1
  •   jcrossley3    15 年前

    我通常只是用 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选项提供更多帮助,但我不熟悉这些选项。