Ripgrep in 30 mins
TL;DR ripgrep is a blazingly fast search tool that combines the usability and
speed.
Introduction#
- In this tutorial, we'll explore why
ripgrepis my search tool and how to use it effectively
Why Ripgrep?#
-
ripgrepstands out for several compelling reasons:- Speed: Written in Rust with highly optimized algorithms,
ripgrepis often 5-10x faster than alternatives - Smart defaults: Automatically respects
.gitignore, skips binary files, and excludes hidden directories - Parallel execution: Searches multiple files simultaneously using all available CPU cores
- Memory efficient: Uses memory-mapped files and streaming search to handle large files gracefully
- Cross-platform: Works seamlessly on Linux, macOS, and Windows
- Rich features: Supports regex, multiline search, file type filtering, and context display
- Speed: Written in Rust with highly optimized algorithms,
-
Here's a speed comparison on a typical codebase:
Tool Time (seconds) Notes grep -r4.2 Basic recursive search ack3.5 Perl-based search rg0.4 Ripgrep with default settings
Installation#
-
Installing
ripgrepis straightforward across all platforms -
On macOS using Homebrew:
-
On Ubuntu/Debian:
-
Verify the installation:
Basic Usage#
-
The simplest
ripgrepcommand searches for a pattern in the current directory:- This recursively searches all files, respecting
.gitignoreand skipping binary files automatically
- This recursively searches all files, respecting
-
Search with case-insensitive matching:
-
Search in a specific directory:
-
Search only in files matching a glob pattern:
-
Show line numbers (enabled by default when output is to terminal):
Understanding the Output#
-
ripgrepoutput is designed for clarity:Output:
src/utils.py 23:def calculate_total(items): 45:def calculate_average(values): src/models.py 12: def calculate_score(self):The format is: - Filename (colored for visibility) - Line number followed by colon - The matching line with pattern highlighted
Advanced Features#
File Type Filtering#
-
ripgrepunderstands common file types:This searches only Python files
-
List all available types:
-
Exclude specific file types:
Context Lines#
-
Show lines before and after matches:
-
Show only lines before:
-
Show only lines after:
Search Only Filenames#
-
List files containing matches without showing the matches:
-
List files not containing matches:
Multiline Search#
- Search across multiple lines:
The
-Uflag enables multiline mode where.matches newlines
Word Boundaries#
- Match whole words only: This prevents matching "word" inside "password" or "wording"
Practical Examples#
Find All TODO Comments#
- Find all TODO comments:
Search for Function Definitions#
- Search for function definitions:
Find All Imports of a Module#
- Find all imports of a module:
Search in Git History#
-
Combine with
gitto search across all branches: -
But for current working tree,
ripgrepis faster:
Find Large Files with Pattern#
- Find large files with pattern:
The
--statsflag shows per-file statistics including file sizes searched
Configuration and Customization#
-
Create a configuration file at
~/.ripgreprc: -
Enable the config file:
Tips and Tricks#
Ignore Additional Patterns#
-
Create a
.rgignorefile in your project root:
Search Hidden Files#
- By default,
ripgrepskips hidden files - Include them:
Search All Files Including Ignored#
- Override
.gitignoreand search everything:
Replace Text Across Files#
- While
ripgrepdoesn't replace text, combine it withsed:
Count Matches#
-
Count occurrences of a pattern:
-
Show total count across all files:
Common Gotchas#
Pattern Escaping#
- Special regex characters need escaping:
Binary Files#
ripgrepskips binary files by default- To search them:
Symbolic Links#
- By default,
ripgrepdoesn't follow symbolic links - To follow them:
Integration with Development Tools#
Vim Integration#
-
Add to
.vimrc: -
Use with:
VS Code Integration#
- VS Code uses
ripgrepby default for file searching - Configure search exclusions in
settings.json:
Command Line Aliases#
-
Add useful aliases to your shell configuration:
Comparison with Other Tools#
Ripgrep vs grep:
| Feature | ripgrep | grep |
|---|---|---|
| Speed | +++ | + |
| Respects .gitignore | Yes | No |
| Parallel search | Yes | Limited |
| Automatic binary exclusion | Yes | No |
| Regex flavor | Rust | POSIX |
Ripgrep vs The Silver Searcher:
| Feature | ripgrep | ag |
|---|---|---|
| Speed | +++ | ++ |
| Memory usage | Lower | Higher |
| Unicode support | Better | Good |
| Active development | Yes | Limited |