Ugrep in Emacs

Almost a year ago I wrote about my handy mu-recursive-grep, an easy way to combine rgrep and vc-git-grep. But after Leah Neukirchen mentioned ugrep recently1, I could not resist checking whether it was a good fit for my daily Emacs usage. A couple of benchmark2 impressed me enough to add support for it directly via xref-search-program-alist.

The details to configure everything you need are over at Using ugrep within Emacs3 on the project README. Once set, you can just hit C-x p g (project-find-regexp) in your project and let ugrep do his magic.

Moreover, as the README mentions, it’s possible to leverage grep-template to have commands like lgrep rely on ugrep.

(setq-default grep-template (string-join '("ugrep"
                                           "--color=always"
                                           "--ignore-binary"
                                           "--ignore-case"
                                           "--include=<F>"
                                           "--line-number"
                                           "--null"
                                           "--recursive"
                                           "--regexp=<R>")
                                         " "))

Thus, mu-recursive-grep becomes a bit simpler:

(defun mu-recursive-grep (search-term search-path)
  "Recursively search for SEARCH-TERM in SEARCH-PATH."
  (interactive
   (progn
     (unless grep-command
       (grep-compute-defaults))
     (let ((search-term (grep-read-regexp))
           (search-path (expand-file-name
                         (read-directory-name
                          "Directory: " nil default-directory t))))
       (list search-term search-path))))
  (lgrep search-term "*" search-path))

As mentioned in Grep’s good rep, for project-wide searches C-x p g is my go-to key binding. However, there are situations when I want to limit the search to specific paths, and that’s where I prefer using mu-recursive-grep.

A closing remark: before blindly leaving grep, ripgrep, or even vc-git-grep behind, I suggest to benchmark the performance of each tool and see what actually suits you best. You may find that whatever you are using at the moment is already the best choice.