Grep's good rep

Look around this blog and you will find nice words about ripgrep. I especially enjoy how I can plug it into Emacs, like the time when I devised my own Helm commands for it.

However, roughly a year ago I started my journey to a more vanilla Emacs experience. During this time I have been slowly moving away from the niceties of ripgrep in order to stick to a couple of built-in tools: rgrep and vc-git-grep. Now, technically speaking, both this commands rely on external tools, grep and git grep. Why should I use these instead of ripgrep? Personal preferences, of course: grep is readily available on Ubuntu and git is the first tool I install when upgrading to a new LTS from Canonical1.

Furthermore, I rarely need the power of ripgrep. Your experience may be different on this, but the size of my projects does not scare neither grep nor git grep. For instance, I regularly use vc-git-grep on Emacs sources, which amounts to something like 2,755,361 lines of code2, and the performances are just fine for me.

The only thing I usually do not need from these commands is the prompt for the file types to look into.

(defun mu-recursive-grep (search-term search-path)
  "Recursively search for SEARCH-TERM in SEARCH-PATH."
     (unless grep-command
     (let ((search-term (grep-read-regexp))
           (search-path (expand-file-name
                          "Directory: " nil default-directory t))))
       (list search-term search-path))))
  (if (vc-root-dir)
      (vc-git-grep search-term "*" search-path)
    (rgrep search-term "*" search-path)))

I combined the two commands to make it simpler. When I am in a Git-versioned project (i.e., vc-root-dir is not nil) mu-recursive-grep runs vc-git-grep; otherwise, it’s time for rgrep to shine. As trivial as it might look, the fact that the last two lines of mu-recursive-grep look similar is what made it easier for me to devise a common wrapper. Emacs describe system is always invaluable for discoveries such as this.

One last thing. When in a project, if I want to find where the thing at point is used I do not need mu-recursive-grep. For this task project-find-regexp (C-x p g) is enough3, but mu-recursive-grep is still helpful when I want to narrow the search down to a specific path.


  1. I use Ansible Playbooks to set up the new system. 

  2. That’s what scc tells me today, anyway. 

  3. C-h v xref-search-program shows that ripgrep can be used instead of the default grep. Hence you can keep ripgrepping if that’s what you prefer.