• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Grep, grep options, and grep’s faster open-source alternative: ripgrep

Posted Dec 24, 2022 — Updated Feb 7, 2024

Tldr:

  • Grep is good but can be slow, especially when searching large codebases
  • Try ripgrep. It’s much faster and has convenient defaults

Grep

In large codebases just finding the things you need can be hard. The built-in solution on linux/unix is grep. The basic format is grep {search_term} {file_path}. Here are a few simple examples:

# This searches for the string "justin" in the file "package.json"
grep justin package.json

# Grep is case-sensitive by default, -i makes it case-insensitive
# So this will find "justin", "Justin", or "JUSTIN"
grep -i justin package.json

# Searches for "justin" in all files in my current directory
grep justin ./*

# "-r" means this will recursively search all directories
# which are children of the current directory.
# This one might take a while
grep -r justin ./*

Here are a few commonly-used useful flags for grep:

FlagEffect
-imakes search case-insensitive
-rrecursively search directories
-nshow the line number within the file where the match occurs
-lreturn only the names of files where matches were found

A quick example combining flags to search the infamous /node_modules directory:

pwd
# /repo/node_modules

# This will print every file name within /node_modules which
# contains a match for the string "justin", case-insensitive
grep -irl justin ./*

When doing a search like this, however, grep is slow. I timed the search above via command line time and it took 34.3s, which is more than enough time to get distracted. I ran the same search with ripgrep and it finished in 3.4s; 10 times faster.

Ripgrep

Ripgrep is an open source alternative to grep, and it is fast.1 I used to worry about optimizing my grep searches to try and speed them up. With rg, I don’t even consider optimization, it’s pointless. Besides winning on speed, ripgrep also bakes in some convenient default search behavior:

  1. Searches the current directory without needing an additional argument
  2. Searches sub-directories, no -r flag needed
  3. Ignores anything in your .gitignore, hidden files, and binary files
  4. Has nicely organized and colorized output, for easier reading

Here’s my local output when running the same query in grep and then again in ripgrep. First, grep:

terminal results of grep
grep terminal results

Now here’s ripgrep (rg). This is the same exact search, but rg doesn’t require the additional -i and -r flags, or the * telling it to search this directory:

terminal results of ripgrep
ripgrep terminal results

Helpful Ripgrep configuration options

In the previous paragraph I mentioned that ripgrep doesn’t require the -i flag, but that’s not entirely true; I just have ripgrep set to --smart-case2 by default. Ripgrep supports a lot of configuration options, including the ability to specify a RIPGREP_CONFIG_PATH pointing to a file containing default options you want all the time. Here’s what’s in my config file, which I lifted directly from the example in the docs:

# Don't let ripgrep vomit really long lines to my terminal, and show a preview.
--max-columns=150
--max-columns-preview

# default all searches to smart case
--smart-case
Zsh

Ripgrep also supports just about every native grep flag. I often use -B or -A to show lines before and after a match for more context.

Conclusion

Grep is useful but slow. There are many open source alternatives to grep, but so far I haven’t found anything that beats ripgrep for pure speed and usability.


Notes

  1. Ripgrep’s README has a number of performance comparisons to other grep-like tools ↩︎
  2. Smart case means: “if the entire string is lowercase, the search is case-insensitive. If anything is uppercased, the search is now case-sensitive.” Just like Vim’s :set smartcase ↩︎

Helpful Links

  • Ripgrep project github page
  • A helpful walkthrough of grep

Filed Under: Command Line, Featured, Productivity, Tools

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy