CuteMachine

Ripgrep Rg

What is ripgrep?

With ripgrep, you can recursively search the current directory for lines matching a pattern.

So ripgrep is just an alternative to the good old grep command. The description in ripgrep's man page says the following:

ripgrep (rg) recursively searches your current directory for a regex pattern. By default, ripgrep will respect your .gitignore and automatically skip hidden files/directories and binary files. ripgrep's regex engine uses finite automata and guarantees linear time searching. Because of this, features like backreferences and arbitrary look around are not supported.

When you search the interwebs for ripgrep, you usually see ripgrep advertised as the fastest alternative to grep, allegedly even faster than the silver searcher (ag) command.

For me, there is no significant difference. ripgrep is an excellent tool and indispensable for my daily work. But I have used silver searcher before using ripgrep, and to be honest, there is no significant difference. So if you use ag already, there might be no reason for you to change.

Fun fact: The rip in ripgrep does not stand for Rest In Peace. According to the developer, Andrew Gallant, the program was never meant to be a grep killer.

Installing ripgrep

The most comfortable possibility to install ripgrep on a Mac is through the Homebrew package manager.

$ brew install ripgrep

After the installation process has finished, you should call the command and print its help section to verify the successful installation.

$ rg --help

Or you can display the man page for ripgrep with the man command. The manual page is only available for the command rg, not for ripgrep.

$ man rg

Using ripgrep

For the examples, let's assume we have the following three files in the current working directory:

$ cat chuckNorris.txt
Chuck Norris counted to infinity … twice.
$ cat key.txt
Press any key to continue or any other key to quit.
$ cat unix.txt
UNIX is the answer, but only if you phrase the question very carefully.

In our first example we want to find all files that include the letter o:

$ rg o
key.txt
1:Press any key to continue or any other key to quit.

chuckNorris.txt
1:Chuck Norris counted to infinity … twice.

unix.txt
1:UNIX is the answer, but only if you phrase the question very carefully.

Sure enough, all three files will be listed with the corresponding matches.

If we only want to see the files names, we can use the -l parameter like so

$ rg -l o
chuckNorris.txt
unix.txt
key.txt

Let's list all files that have the word UNIX in it.

$ rg -l UNIX
unix.txt

$ rg -l --files-with-matches UNIX
unix.txt

Now, let's list all files that do not have the word UNIX in it.

$ rg -l --files-without-match UNIX
chuckNorris.txt
key.txt

If you only want to search in certain file types, you can use the -t parameter. You can see a list of all supported file types with the --type-list argument:

$ rg --type-list

To search in Markdown files, use the rg -t md command. To search in JavaScript files, use rg -t js.

Another essential parameter I use all the time when looking for React components in my code base is the --multiline --multiline-dotall option. Using this option will allow the regular expression to match across multiple lines. Components typically span over multiple lines, so this comes in very handy when working with a React codebase.

To see this in action, suppose we have the following file contents in strawberry.txt:

$ cat strawberry.txt
strawberry
fields
forever
$ rg 'straw.*fields' --multiline --multiline-dotall
strawberry.txt
1:strawberry
2:fields

We need the --multiline-dotall option because the regular expression .* does not match the newline character.

When we search for the term fields in strawberry.txt we will only get the match:

$ cat strawberry.txt | rg fields
fields

Sometimes we also want to see the line(s) before and/or after the match. We can do this with the options -A and -B.

$  cat strawberry.txt |rg fields -B 1
strawberry
fields

 $ cat strawberry.txt |rg fields -A 1
fields
forever

Sometimes you need to edit the files that did match. You can do this easily when you combine rg with vi using a Zsh function. Add this to your .zshrc file to make a new zsh function available.

function rgvi() {
  vi $(rg -l $1)
}

When you source your .zshrc file with source .zshrc, you can use the rgvi function.

$ rgvi straw

The above command will open the file in vi.

Posted on CuteMachine.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer