【问题标题】:How to grep current word under cursor in emacs?如何在emacs中grep光标下的当前单词?
【发布时间】:2016-01-09 21:15:03
【问题描述】:

我正在尝试在 emacs 中 grep 光标下的当前单词。我在下面编写了代码,它引发了错误:参数数量错误...当我从 xx() 中删除 (grep) 并将其绑定到 f4 键(最后一行注释)时,在 f3 和 f4 grep 搜索光标下的单词之后。有谁知道为什么不能从 xx() 调用 (grep)? 谢谢,亚历克斯。

(require 'grep)
(defun xx ()
  "setting up grep-command using current word under cursor as a search string"
 (interactive)
 (setq curWord (thing-at-point 'word))
 (setq VALUE   (concat "grep -nH -r --exclude='TAGS' --include='*.h' --include='*.cpp' --include='*.pl' --include='*.c' -e " curWord " /home/alex/code/") )
 (grep-apply-setting 'grep-command VALUE)
 (grep))

 (global-set-key (kbd "<f3>")  'xx)
;(global-set-key (kbd "<f4>") 'grep )

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    grep 函数接受一个参数,因此请更改您的函数以传递它:

    (defun xx ()
      "setting up grep-command using current word under cursor as a search string"
      (interactive)
      (let* ((cur-word (thing-at-point 'word))
             (cmd (concat "grep -nH -r --exclude='TAGS' --include='*.h' --include='*.cpp' --include='*.pl' --include='*.c' -e " cur-word " /home/alex/code")))
        (grep-apply-setting 'grep-command cmd)
        (grep cmd)))
    

    【讨论】:

      【解决方案2】:

      您无需更改grep 命令或定义您自己的替换。你只需要获取它提供的默认值,这已经是那个词了。

      1. 在您使用M-x grep 之后,您应该能够使用M-n 来检索该点的单词。 (但要小心放回开关。显然,当您点击 M-n 时,香草 Emacs 会删除它们。)

        M-n 在提示符处将命令的默认值插入到迷你缓冲区中。如果有多个默认值,则在其中重复 M-n 循环。这通常是正确的,不仅仅是命令grep

      2. 如果您使用库 Grep+,则默认值会自动插入到 minibuffer 中(无需更改命令开关)。而且您可以更好地控制您想要的默认值和其他默认行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 2011-06-07
        • 2014-03-07
        • 2010-12-19
        • 1970-01-01
        相关资源
        最近更新 更多