如何在emacs中grep光标下的当前单词?

如何在emacs中grep光标下的当前单词?,emacs,elisp,Emacs,Elisp,我试图在emacs中将当前单词放在光标下。我写了下面的代码,它抛出了一个错误:参数的数目错误。。。当我从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" (i

我试图在emacs中将当前单词放在光标下。我写了下面的代码,它抛出了一个错误:参数的数目错误。。。当我从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 )
(需要“grep”)
(defun xx()
“使用光标下的当前单词作为搜索字符串设置grep命令”
(互动)
(setq curWord(点上的事物词))
(setq值(concat“grep-nH-r--exclude='TAGS'--include='*.h'--include='*.cpp'--include='*.pl'--include='*.c'-e“curWord”/home/alex/code/))
(grep应用设置“grep命令值”)
(grep))
(全局设置键(kbd“”)'xx)
;(全局设置键(kbd“”)“grep)

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)))

您无需更改
grep
命令或定义自己的替换项。您只需要获取它提供的默认值,它已经是当前的单词

  • 在使用
    M-x grep
    之后,您应该能够使用
    M-n
    在点处检索单词。(但要小心放回开关。显然,当你点击
    M-n
    时,香草Emacs会将开关移除)

    M-n
    在提示符处将命令的默认值插入到微型缓冲区中。如果有多个默认值,则在它们之间重复
    M-n
    循环。这在一般情况下是正确的,不仅仅是命令
    grep

  • 如果使用库,则默认值将自动插入微型缓冲区(无需更改命令开关)。您可以更好地控制默认值和其他默认行为