Skip to content

Search file contents

[ This is not needed given that we provide the denote-grep command (Use denote-grep to search inside files). ]

Emacs provides built-in commands which are wrappers of standard Unix tools: ‘M-x grep’ lets the user input the flags of a grep call and pass a regular expression to the ‘-e’ flag.

The author of Denote uses this thin wrapper instead:

emacs-lisp
(defvar prot-search--grep-hist '()
  "Input history of grep searches.")

;;;###autoload
(defun prot-search-grep (regexp &optional recursive)
  "Run grep for REGEXP.

Search in the current directory using `lgrep'.  With optional
prefix argument (\\[universal-argument]) for RECURSIVE, run a
search starting from the current directory with `rgrep'."
  (interactive
   (list
    (read-from-minibuffer (concat (if current-prefix-arg
                                      (propertize "Recursive" 'face 'warning)
                                    "Local")
                                  " grep for PATTERN: ")
                          nil nil nil 'prot-search--grep-hist)
    current-prefix-arg))
  (unless grep-command
    (grep-compute-defaults))
  (if recursive
      (rgrep regexp "*" default-directory)
    (lgrep regexp "*" default-directory)))

Rather than maintain custom code, consider using the excellent ‘consult’ package: it provides commands such as consult-grep and consult-find which provide live results and are generally easier to use than the built-in commands.