Rest in peace, grep!

I’m not saying that grep is slow or flawed in any way, but it can definitely be faster (and better). Plus, GNU grep is not the only player out there.

Let me introduce ripgrep, a grep/ag/ack alternative written in Rust.

So, why should you use ripgrep? Because it’s fast. Very fast! It has saner defaults, and it’s written in Rust, but that’s a topic for another time. :)

Also, I just learned that ripgrep powers Visual Studio Code’s search.

Benefits

  • Did I tell you that ripgrep is crazy fast?
  • It searches recursively by default.
  • It ignores hidden and binary files by default.
  • It respects .gitignore. It will skip listed files and directories by default.
  • You can restrict your search to specific filetypes.
  • It prints pretty.
  • It supports file encodings other than UTF-8.
  • It’s crazy fast.

Gotchas

  • ripgrep doesn’t have multiline search.
  • Since it heavily uses threads to do work, ripgrep’s output is not deterministic (Tip: pipe the output through sort).

It’s not a drop-in replacement for GNU grep or ag, though. So, don’t replace them with rg in scripts without testing.

Installation

If you have Rust toolchain (1.20 or newer) installed, you can install it using cargo. Add ~/.cargo/bin to $PATH if you haven’t yet.

$ cargo install ripgrep

If you’re working in bleeding edge Arch, run:

$ pacman -S ripgrep

On macOS, run:

$ brew install ripgrep

If you are worried about having to type three more letters every time you search and don’t know what an alias is, don’t worry. The binary is just called rg. (:

Usage

If you know how to use grep, you can use ripgrep. I’ll outline some basic usage here though. Read the instructions on its GitHub page if you want to know everything about it.

To search for any word recursively in a directory:

$ rg <keyword>

ripgrep's default behavior is to skip hidden and binary files apart from everything ignored by git. Use -uuuto disable that.

To search for a keyword in only specific filetypes, pass the file extension to the -t switch:

$ rg -tjs foo

To search for a keyword in files matching the specified glob:

$ rg foo -g 'bar.*'

Basic Benchmarks

I ran simple benchmarks on my machine (Core i7 6500U, 8GB RAM, KDE neon 5.12.5 based on Ubuntu 16.04) using /usr/bin/time binary, and ripgrep seems to beat GNU grep everytime (by a huge margin!).

$ /usr/bin/time rg -uu import > /dev/null # ~24 seconds
$ /usr/bin/time grep -r import * > /dev/null # ~3 min 27 seconds

Keep in mind that these are not scientific benchmarks by any means. Go to ripgrep’s GitHub page for more comprehensive numbers.

Attributions:
  1. undefined by undefined