What it is
ripgrep is a blazingly fast, line-oriented search tool that recursively searches the current directory for a regex, respecting .gitignore and other ignore files by default.
Installation
Linux
# Debian/Ubuntu
sudo apt update && sudo apt install ripgrep
# Fedora
sudo dnf install ripgrep
# Arch Linux
sudo pacman -S ripgrep
macOS
# Homebrew
brew install ripgrep
Windows
# Scoop
scoop install ripgrep
# Chocolatey
choco install ripgrep
Core Concepts
ripgrep’s primary advantage comes from its intelligent use of ignore files. It automatically respects:
.gitignorefiles.ignorefiles.rgignorefilesgit:ignoreconfiguration in.git/confighg:ignoreconfiguration in.hg/hgrc
This means that by default, you won’t search ignored files, making searches much faster and more relevant.
Commands / Usage
Basic Searching
-
Search for a literal string:
rg "hello world"Searches for the exact string "hello world" in all files in the current directory and its subdirectories.
-
Search for a regular expression:
rg "error [0-9]+"Searches for lines containing "error" followed by one or more digits.
-
Case-insensitive search:
rg -i "debug"Searches for "debug", "Debug", "DEBUG", etc.
-
Search for a whole word:
rg -w "the"Searches for the word "the" and not "there" or "other".
-
Search for a specific file type:
rg -t py "import requests"Searches for lines containing "import requests" only in Python files (
.py). -
Search for multiple file types:
rg -t {js,ts} "console.log"Searches for lines containing "console.log" in JavaScript (
.js) and TypeScript (.ts) files. -
Search across all files (ignoring ignore files):
rg -u "config_value"Searches for "config_value" in all files, including those that would normally be ignored.
-
Search across all files, including hidden files (but still respecting ignore files):
rg -uu "secret_key"Searches for "secret_key" in all files, including hidden ones, but still respects ignore files.
-
Search across all files, including hidden and ignored files:
rg -uuu "old_api_endpoint"Searches for "old_api_endpoint" in all files, including hidden and ignored ones.
Filtering by File Path
-
Search only in specific files:
rg "function setup()" --glob "src/**/*.js"Searches for "function setup()" only within JavaScript files in the
srcdirectory and its subdirectories. -
Search only in files matching a glob pattern:
rg "TODO" --glob "*.md"Searches for "TODO" only in Markdown files (
.md). -
Exclude specific directories:
rg "database_connection" --glob '!db/*'Searches for "database_connection" but excludes any files within the
dbdirectory. -
Exclude specific files:
rg "license_key" --glob '!third-party/*'Searches for "license_key" but excludes any files within the
third-partydirectory. -
Search in files NOT matching a glob pattern:
rg "deprecated_function" --glob '!tests/*'Searches for "deprecated_function" in all files except those in the
testsdirectory.
Output Control
-
Show line numbers:
rg -n "important_variable"Prints the line number for each match.
-
Show column numbers:
rg -o --column "pattern"Prints the column number for each match. The
-oflag prints only the matching text. -
List only files containing matches:
rg -l "error message"Prints only the names of files that contain the string "error message".
-
List only files NOT containing matches:
rg -L "success"Prints only the names of files that do NOT contain the string "success".
-
Print context lines before and after the match:
rg -C 3 "critical_section"Prints 3 lines of context before and after each match for "critical_section".
-
Print context lines after the match:
rg -A 2 "start_process"Prints 2 lines of context after each match for "start_process".
-
Print context lines before the match:
rg -B 1 "end_process"Prints 1 line of context before each match for "end_process".
-
Show path to the file:
rg --with-filename "configuration_setting"Always prints the filename before the matching line (useful when searching a single file to differentiate output). This is the default behavior when searching multiple files.
-
Disable path display:
rg --no-filename "single_match"Disables printing the filename before the matching line.
-
Disable line number display:
rg --no-linenum "plain_text"Disables printing line numbers.
-
Show only the matching part of the line:
rg -o "regex_match"Prints only the substring that matched the regex, not the entire line.
-
Show all matches on a single line:
rg -Q "unique_id"Prints each match on its own line, even if the match spans multiple lines in the original file.
-
Colorize output:
rg --color=always "important_keyword"Forces colorization of the output. Useful when piping to other commands or redirecting to a file where you want ANSI color codes.
-
Disable colorization:
rg --color=never "plain_output"Disables colorization of the output.
Advanced Searching
-
Search for a literal string, not a regex:
rg -F "literal string with symbols *"Treats the search pattern as a fixed string, not a regular expression.
-
Use a file containing patterns:
rg -f patterns.txtReads search patterns from the file
patterns.txt, one pattern per line. -
Use a file containing file paths to search:
rg -f file_list.txtSearches only in the files listed in
file_list.txt, one path per line. -
Search in binary files:
rg -a "binary_data_pattern"Processes binary files as if they were text. Use with caution as it might produce garbled output.
-
Search for empty lines:
rg ^$Finds all empty lines.
-
Search for lines containing only whitespace:
rg '^\s*$'Finds all lines that contain only whitespace characters.
Configuration
- Show the configuration
ripgrepis using:
Prints the configurationrg --configripgrepis currently using, including loaded ignore files and command-line flags.
Common Patterns
-
Find all TODOs in the project and show line numbers:
rg -n "TODO" -
Search for a specific function definition in JavaScript files:
rg -t js "function myFunction(" -
Find all occurrences of a deprecated API call in Python code, excluding tests:
rg "old_api_call" --glob '!tests/*' -t py -
List all files that contain the string "sensitive_info":
rg -l "sensitive_info" -
Find all files containing "error" and pipe the list of files to
xargsto delete them:rg -l "error" | xargs rmCaution: Use with extreme care!
-
Search for a pattern and count the total number of matches:
rg "pattern" | wc -l -
Search for a pattern, ignoring case, and show only matching files:
rg -i -l "config_setting" -
Search for a specific pattern in markdown files and show context:
rg -C 2 --glob '*.md' "important_note" -
Search for a pattern across all files, including hidden ones, but excluding a specific directory:
rg -uu "debug_log" --glob '!logs/*'
Gotchas
- Default behavior with Git:
ripgreprespects.gitignoreby default. If you want to search files that are ignored by Git, you need to use the-uflags (-u,-uu, or-uuu). - Binary files: By default,
ripgrepskips binary files to avoid corrupting your terminal. If you need to search binary files, use the-aflag. - Performance on huge directories: While
ripgrepis fast, searching extremely large directories with many files might still take time. Using--globto narrow down the search scope can help. - Regex syntax:
ripgrepuses Rust’s regex engine, which is PCRE-like. Most common regex patterns will work as expected, but be aware of subtle differences if you’re coming from a different regex engine. - Glob syntax:
ripgrepuses glob patterns for file path filtering. The syntax is generally intuitive (e.g.,*.js,src/), but consultripgrep’s documentation for advanced globbing features. - Combining
-uflags:-u: Search hidden files, but respect ignore files.-uu: Search hidden files and files that are ignored by ignore files.-uuu: Search all files, including hidden and ignored files. This is the most comprehensive but slowest option.