Most developers install ripgrep (rg), run rg "some_function", see fast results, and never explore further.
That is leaving 80% of its utility on the table. Written in Rust by Andrew Gallant, ripgrep is not just a faster grep; it is a full replacement for combinations of grep, find, and sed.
Below is a high-density, copy-pasteable cheat sheet covering the flags, syntax, and configuration recipes that save real engineering hours every week.
Search only inside Python files, or exclude Markdown entirely:
rg -T md 'TODO'
Search inside .gitignore rules, or include hidden dotfiles and binaries:
rg -uu 'pattern' # Searches hidden (.env) too
Find functions or structs spanning across multiple lines:
Show 2 lines Before (-B), After (-A), or surrounding (-C):
List only the filenames that contain the string:
Capture groups and replace string in the printed stdout:
The Essential .ripgreprc Configuration File
Instead of typing --smart-case or your favorite exclusions every time, ripgrep reads an environment variable pointing to a global config file.
Create ~/.config/ripgrep/config:
# Search case-insensitively if query is lowercase, sensitive if uppercase
--smart-case
# Follow symlinks
--follow
# Display line numbers always
--line-number
# Don't let giant minified vendor files stall your search
--max-columns=150
--max-columns-preview
# Common directories to always exclude
--glob=!node_modules/*
--glob=!.git/*
--glob=!dist/*
--glob=!target/*
--glob=!.next/*
--glob=!.venv/*
# Custom file type definition for Docker
--type-add=docker:Dockerfile*
--type-add=docker:docker-compose*.{yml,yaml}
Then add this export to your ~/.zshrc or ~/.bashrc:
export RIPGREP_CONFIG_PATH="$HOME/.config/ripgrep/config"
The Interactive Terminal Superpower: Piping rg into fzf
If you have fzf and bat installed, you can combine them with ripgrep into a real-time, interactive code search tool that previews code in your terminal:
# Add this function to your ~/.zshrc or ~/.bashrc:
rgi() {
rg --color=always --line-number --no-heading --smart-case "${*:-}" |
fzf --ansi --color "hl:-1:underline,hl+:-1:underline:reverse" --delimiter : --preview 'bat --color=always {1} --highlight-line {2}' --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' --bind 'enter:become(vim +{2} {1})'
}
Now, running rgi "auth_token" launches an interactive terminal picker. As you type or arrow up/down, bat shows the exact lines in context, and hitting Enter immediately opens your editor at that exact line!
Gotcha: When to Avoid --mmap
ripgrep supports a --mmap flag to memory-map files instead of reading them into a buffer. While this can provide a tiny speedup on local NVMe SSDs, never use --mmap on network filesystems (NFS, SMB, or SSHFS). If another process or server modifies or truncates the file while ripgrep's memory map is open, your operating system will crash the search process with a SIGBUS (Bus Error) signal.
Discussion & Issues
Comments
Post a Comment