【问题标题】:Emacs find-grep-dired then automatically isearch-forward on given regexpEmacs find-grep-dired 然后在给定的正则表达式上自动 isearch-forward
【发布时间】:2011-02-11 13:11:20
【问题描述】:

我通常使用find-grep-dired 在项目目录中查找表达式。这给了我一个很好的dired 包含该表达式的所有文件的视图。但我的下一步总是打开其中一个文件并使用相同的搜索表达式执行isearch-forward。如何避免每次输入两次搜索词(如果我要编辑多个文件,则输入两次以上)?

【问题讨论】:

    标签: emacs grep dired


    【解决方案1】:

    这应该适合你:

    1. 照常运行find-grep-dired
    2. * t(dired-toggle-marks)标记所有文件。
    3. A 启动dired-do-search。出现提示时,不要键入,而是按 M-p,这将打开您的 find-grep 正则表达式,因为这两个函数使用相同的提示历史列表
    4. 您将被带到第一个文件中的第一个匹配项。这是有趣的部分,只需按 M-, 即可转到包含所有匹配文件的下一个匹配项。
    5. 利润? (抱歉,忍不住)

    如果您想一口气完成所有操作,那就去吧:

    (defun find-grep-dired-do-search (dir regexp)
      "First perform `find-grep-dired', and wait for it to finish.
    Then, using the same REGEXP as provided to `find-grep-dired',
    perform `dired-do-search' on all files in the *Find* buffer."
      (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
      (find-grep-dired dir regexp)
      (while (get-buffer-process (get-buffer "*Find*"))
        (sit-for 1))
      (with-current-buffer "*Find*"
        (dired-toggle-marks)
        (dired-do-search regexp)))
    

    【讨论】:

    • dired-do-search 在 dired 缓冲区中绑定到 A
    • M-,前进,后退的键位是什么?即提前发生?
    【解决方案2】:

    您可以将 find-grep-dired 中使用的搜索字符串存储在 kill ring (C-SPACE C-a M-w) 中。然后使用 kill ring 中的字符串 (C-s M-y) 在文件中进行搜索。 M-y 将拉出最后一串被杀死的文本。

    您可以使用 C-h k C-s 显示 isearch-forward 的其他(有用的)绑定。

    【讨论】:

    • 谢谢,这是一种方法,当然。我希望涉及的东西少一点,比如对最后一个搜索字符串的 isearch 的单个绑定。
    【解决方案3】:

    换一种方法怎么样?尝试使用 igrep.el 包中的 M-x igrep-find

    默认情况下,它会搜索模式的 所有 次出现,但您可以更改行为以查找第一个这样的出现:

    (setq igrep-options "-i -m 1")   ;; I like -i for case-insensitivity
    

    这将导致 编译 样式缓冲区(命名为 *igrep*),每个文件只有一行,当您单击该行时(或执行 C-x `),你会自动被放到匹配的那一行。另外,您可以在*igrep* 缓冲区中看到匹配行。

    显然,如果您想在每个文件中查看多个匹配项,请更改 -m 之后的数字,或完全省略该部分。

    【讨论】:

    • 我试过了,但出错了:find /Users/reed/sb/ampms/app -type d (-name RCS -o -name CVS -o -name SCCS) -prune -o -输入 f \! -name *\~\! -name *\,v \! -name s.* \! -name .\#* -print0 | xargs -0 -e grep -n -e test /dev/null xargs: 非法选项 --e 用法: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr] [ -L number] [-n number [-x]] [-P maxprocs] [-s size] [utility [argument ...]]
    • @Reed 看起来你的 xargs 不支持-e,不知道为什么。您可以通过设置 (setq igrep-find-use-xargs nil) 来避免使用 xargs。或者下载更新版本的 xargs(在 findutils 中)。
    • 我在 OSX 上。也许这就是原因。
    • 我从 homebrew:brew install findutils 安装了 findutils 并更改了我的 $PATH 以使 /usr/local/bin/xargs 在系统之前加载 /usr/bin/xargs 像这样 export PATH=/usr/local/bin:/usr/local/sbin:$PATH 现在效果很好。我真希望它可以使用 ido-mode 来查找起始搜索目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多