DEV Community

Abhinav Kumar
Abhinav Kumar

Posted on • Edited on • Originally published at abhnv.com

Rest in peace, grep!

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.20 or newer) installed, you can install it using cargo. Add ~/.cargo/bin to $PATH if you haven't yet.

$ cargo install ripgrep
Enter fullscreen mode Exit fullscreen mode

If you run bleeding-edge Arch, run

$ pacman -S ripgrep
Enter fullscreen mode Exit fullscreen mode

Fedora users can install it using:

$ dnf install ripgrep
Enter fullscreen mode Exit fullscreen mode

On macOS, run

$ brew install ripgrep
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To search a keyword in files matching the specified glob:

$ rg foo -g 'bar.*'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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


This post was originally published on my website.

Top comments (3)

Collapse
 
svnset profile image
Robin L. • Edited

Yep rg is awesome. rg + fzf have changed my worklfow in terminal and vim quite alot. They showcase the capabilities of go and rust very nice. Just a little hint about Fedora, since 27 it's in the official repos so you can just :

sudo dnf install ripgrep

Collapse
 
abhinav profile image
Abhinav Kumar • Edited

Thanks! I didn't know that.

I have updated the post.

Collapse
 
maxdevjs profile image
maxdevjs

With a very scientific one-shot benchmark, ag proved to be the winner (:D)