ripgrep: The Fastest Code Search Tool

⬅️ Back to Tools

Overview

ripgrep (rg) is a line-oriented search tool that recursively searches directories for a regex pattern while respecting your .gitignore files. Written in Rust by Andrew Gallant, it combines the usability of tools like The Silver Searcher (ag) with the raw performance of GNU grep.

With many GitHub stars, ripgrep has become the standard code search tool for developers who care about speed and correctness.

Why ripgrep

  • Fast: built on Rust’s regex engine with SIMD optimizations and aggressive literal extraction. Outperforms grep, ag, git grep, and every other code search tool in most benchmarks
  • Smart by default: respects .gitignore, skips hidden and binary files, and searches recursively
  • Unicode always on: stays fast with full Unicode support, unlike GNU grep which slows down dramatically in Unicode mode
  • Cross-platform: precompiled binaries for Linux, macOS, and Windows
  • Feature-rich: file type filtering (rg -tpy), search and replace, multiline search, compressed file support, PCRE2 opt-in, and more

Benchmarks

From Andrew Gallant’s blog post on ripgrep, searching the Linux kernel source tree for [A-Z]+_SUSPEND:

ToolTimeRelative
ripgrep0.082s1.00x
hypergrep0.167s2.04x
git grep0.273s3.34x
The Silver Searcher (ag)0.443s5.43x
ugrep0.639s7.82x
git grep (Unicode)2.670s32.70x
ack2.935s35.94x

On a 13GB single file, searching for Sherlock [A-Z]\w+:

ToolTimeRelative
ripgrep1.042s1.00x
ugrep1.339s1.28x
GNU grep (Unicode)6.577s6.31x

The blog post contains 25+ benchmarks with detailed analysis covering literal search, case-insensitive search, alternations, Unicode, single-file performance, and more.

Installation

The binary name is rg.

macOS (Homebrew):

brew install ripgrep

Arch Linux:

sudo pacman -S ripgrep

Debian/Ubuntu:

sudo apt-get install ripgrep

Fedora:

sudo dnf install ripgrep

Cargo:

cargo install ripgrep

Windows (Chocolatey):

choco install ripgrep

Windows (Scoop):

scoop install ripgrep

Precompiled binaries for all platforms are available on the releases page.

Quick Examples

# Recursive search in current directory
rg foobar

# Case insensitive
rg -i foobar

# Search only Python files
rg -tpy foobar

# Exclude JavaScript files
rg -Tjs foobar

# Show 3 lines of context
rg -C3 foobar

# Search and replace
rg '([A-Z][a-z]+)\s+([A-Z][a-z]+)' --replace '$2, $1'

# Word boundary matching
rg -w foo

# Ignore gitignore (search everything)
rg -uu foobar

# Search compressed files
rg -z foobar

Key Features

Automatic Filtering

By default, ripgrep skips:

  • Files listed in .gitignore, .ignore, and .rgignore
  • Hidden files and directories
  • Binary files

This makes results more relevant compared to grep -r, which searches everything.

File Type Filtering

rg -tpy foo          # Search only Python files
rg -thtml -tcss foo  # Search HTML and CSS files
rg --type-list       # List all supported types
rg --type-add 'foo:*.{foo,foobar}' -tfoo bar  # Add custom type

PCRE2 Support

For lookaround and backreferences, opt in with -P:

rg -P '(?<=foo)bar'

Compressed File Search

Search inside gzip, xz, lzma, bzip2, lz4, brotli, and zstandard files:

rg -z foobar

Preprocessing Filters

Run arbitrary preprocessing on files before searching:

rg --pre pdf2text foobar *.pdf

Why It’s Fast

ripgrep’s speed comes from several layers:

  1. Rust’s regex engine: uses finite automata with SIMD and aggressive literal optimizations. Extracts prefix, suffix, and inner literals from patterns to skip through files without entering the regex engine
  2. Literal-first search: when a pattern reduces to a literal or alternation of literals, the core regex engine is bypassed entirely using SIMD-accelerated matching (the “Teddy” algorithm from Intel’s Hyperscan)
  3. Incremental search: reads files in constant-sized buffers rather than line-by-line or memory-mapping entire files, avoiding memory pressure on large repos
  4. Lock-free parallelism: uses a Chase-Lev work-stealing queue for distributing search work across threads
  5. Minimum stat calls: recursive directory iterator makes the minimum number of system calls possible

For the full technical breakdown, see the blog post.

Links

License

Dual-licensed under MIT or the Unlicense.