ripgrep: The Fastest Code Search Tool
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:
| Tool | Time | Relative |
|---|---|---|
| ripgrep | 0.082s | 1.00x |
| hypergrep | 0.167s | 2.04x |
| git grep | 0.273s | 3.34x |
| The Silver Searcher (ag) | 0.443s | 5.43x |
| ugrep | 0.639s | 7.82x |
| git grep (Unicode) | 2.670s | 32.70x |
| ack | 2.935s | 35.94x |
On a 13GB single file, searching for Sherlock [A-Z]\w+:
| Tool | Time | Relative |
|---|---|---|
| ripgrep | 1.042s | 1.00x |
| ugrep | 1.339s | 1.28x |
| GNU grep (Unicode) | 6.577s | 6.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 ripgrepArch Linux:
sudo pacman -S ripgrepDebian/Ubuntu:
sudo apt-get install ripgrepFedora:
sudo dnf install ripgrepCargo:
cargo install ripgrepWindows (Chocolatey):
choco install ripgrepWindows (Scoop):
scoop install ripgrepPrecompiled 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 foobarKey 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 typePCRE2 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 foobarPreprocessing Filters
Run arbitrary preprocessing on files before searching:
rg --pre pdf2text foobar *.pdfWhy It’s Fast
ripgrep’s speed comes from several layers:
- 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
- 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)
- Incremental search: reads files in constant-sized buffers rather than line-by-line or memory-mapping entire files, avoiding memory pressure on large repos
- Lock-free parallelism: uses a Chase-Lev work-stealing queue for distributing search work across threads
- Minimum stat calls: recursive directory iterator makes the minimum number of system calls possible
For the full technical breakdown, see the blog post.
Links
- GitHub: BurntSushi/ripgrep
- Blog post: ripgrep is faster than {grep, ag, git grep, ucg, pt, sift}
- User guide: GUIDE.md
- FAQ: FAQ.md
- Feature comparison: beyondgrep.com/feature-comparison
License
Dual-licensed under MIT or the Unlicense.