Abhinav Kumar's Website<!-- --> | <!-- -->Rest in peace, grep!

Rest in peace, grep!

Posted on May 16, 2018 by Abhinav Kumar in blog

I'm not saying that grep is slow or flawed in any way but it can be definitely faster (and better). And 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. (Topic for another time:)

Also, I just learnt that ripgrep powers Visual Studio Code's search.

Benefits

  • Did I tell you that it's 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

  • Doesn't have multiline search.
  • Since it uses threads heavily 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.12 or newer) installed, you can install it using cargo. Add ~/.cargo/bin to $PATH if you haven't yet.

$ cargo install ripgrep

If you run bleeding-edge Arch, run

$ pacman -S ripgrep

Fedora users can install it using:

$ dnf copr enable carlgeorge/ripgrep
$ dnf install ripgrep

On macOS, run

$ brew install ripgrep

If you are worrying about having to type 3 more letters everytime you search and don't know what an alias is, don't worry. The binary is called just rg. (:

Usage

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

To search 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 -uuu to disable that.

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

$ rg -tjs foo

To search 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 reports.