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.
.gitignore
. It will skip listed files and directories by default.sort
).It’s not a drop-in replacement for GNU
grep
orag
, though. So, don’t replace them withrg
in scripts without testing.
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
. (:
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-uuu
to 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.*'
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.
Free Resources