Search code examples
emacselisp

How to grep current word under cursor in emacs?


I am trying to grep current word under cursor in emacs. I have written code below, which throws the error: Wrong number of arguments... When I remove (grep) from xx() and bind it to f4 key (commented last line), after f3 and f4 grep searches word under cursor. Does any one know why (grep) can not be called from xx()? Thanks, Alex.

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

Solution

  • The grep function takes one argument, so change your function to pass it:

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