This should return a path to the rg executable. If it doesn’t, figure out where your installer put the executable and add that folder to your $PATH .

Tell Emacs to use ripgrep instead of its default grep command. Add the following to your ~/.emacs file, your ~/.emacs.d/init.el file, or wherever you put your custom Emacs configuration

(grep-apply-setting 'grep-find-command '("rg -n -H --no-heading -e '' $(git rev-parse --show-toplevel || pwd)" . 27) )

There’s a lot going on here, so let’s unpack it. At a high level, we’re using the grep-apply-setting function to tell Emacs what command should be run when we run M-x grep-find . The value that we’re passing is this complicated object '("rg -n -H --no-heading -e '' $(git rev-parse --show-toplevel || pwd)" . 27) . Let’s look at what that means.

To start, we’re calling rg (this was our main goal: to call ripgrep).

-n tells ripgrep to display the line number for each match (the line in the file on which the match appeared). -H tells ripgrep to display the file name for each match. --no-heading tells ripgrep to display the file name on each line, rather than grouping matches by file and only displaying the filename at the top of each group. This is important because Emacs will only let us jump to a match if we have the complete file path and line number all together.

-e tells ripgrep that the next argument should be regular expression to search for. In this command, we start with an empty regular expression ( '' ), which we will fill in whenever we call M-x grep-find .

The final argument we supply to rg is the directory to search. rg will default to searching the current directory, but I want it to check the entire current Git repo (I want to see results from my whole project, not just the directory of the file I happen to have open). If we’re in a Git repo we can use git rev-parse --show-toplevel to get the repo’s root directory, but this command will return an error if we’re not in a Git repo. We could just leave it there, but in the rare case that I’m searching outside of a Git repo I don’t want this command to return an error.

Bash (like most shells) allows us to recover from errors using the || operator. cmd1 || cmd2 will run cmd1 , then run cmd2 if cmd1 returned an error. In this case, our second command is pwd , which will print the directory we’re currently in ( pwd is short for Print Working Directory). So $(git rev-parse --show-toplevel || pwd) will be the root directory of the current Git repo if we’re in a Git repo, or the current directory if we’re not.