Utility Modes¶
Beyond pattern searching, ripgrep provides several utility modes for discovery, shell integration, and diagnostics. These modes help you explore available features, generate shell completions, and integrate ripgrep into your development workflow.
Overview¶
Utility modes are special operating modes where ripgrep doesn't search for patterns but instead provides information or generates configuration files. These modes are essential for:
- Discovering which files would be searched
- Listing available file types
- Generating shell completions
- Creating man pages
- Checking version and feature availability
flowchart LR
Start[ripgrep command] --> Mode{Utility Mode?}
Mode -->|--files| Files[List Files Mode]
Mode -->|--type-list| Types[Type List Mode]
Mode -->|--generate| Gen[Generation Mode]
Mode -->|--version/-V| Ver[Version Mode]
Mode -->|--pcre2-version| PCRE[PCRE2 Check]
Mode -->|pattern| Search[Search Mode]
Files --> FilesOut[Output: File paths]
Types --> TypesOut[Output: Type definitions]
Gen --> GenType{Generator Type?}
Ver --> VerOut[Output: Version info]
PCRE --> PCREOut[Output: PCRE2 status]
GenType -->|complete-bash| Bash[Bash Completions]
GenType -->|complete-zsh| Zsh[Zsh Completions]
GenType -->|complete-fish| Fish[Fish Completions]
GenType -->|complete-powershell| PS[PowerShell Completions]
GenType -->|man| Man[Man Page]
style Files fill:#e1f5ff
style Types fill:#e1f5ff
style Gen fill:#fff3e0
style Ver fill:#e1f5ff
style PCRE fill:#e1f5ff
style Search fill:#f3e5f5
Figure: Utility mode selection flow showing how flags determine ripgrep's operating mode.
Files Mode¶
The --files flag lists all files that ripgrep would search, without performing any pattern matching.
Basic Usage¶
# List all files that would be searched
rg --files
# List files in specific directory
rg --files /path/to/directory
Use Cases¶
Best Practice
Always use --files to preview search scope before running expensive queries on large codebases. This helps verify your filters are working correctly.
Preview Search Scope:
Verify File Filtering:
# Check which files match your type filter
rg --files -t rust
# Verify ignore rules are working
rg --files --no-ignore
Generate File Lists:
# Create a file inventory
rg --files > project-files.txt
# Find files modified recently (combine with other tools)
rg --files | xargs ls -lt | head -20
Pipe to Other Tools:
# Count lines in all searchable files
rg --files | xargs wc -l
# Find largest files in project
rg --files | xargs du -h | sort -rh | head -10
Combining with Filters¶
The --files mode respects all filtering options:
# List only Python files
rg --files -t py
# List files matching glob pattern
rg --files -g '*.json'
# List files including hidden ones
rg --files --hidden
# List files ignoring .gitignore
rg --files --no-ignore
Flag Precedence
The --files flag takes precedence over --type-list. If you specify both flags, ripgrep will list files rather than displaying type definitions.
Source: crates/core/flags/defs.rs:2151
Type List Mode¶
The --type-list flag displays all built-in file type definitions.
Basic Usage¶
Output Format¶
Each line shows a file type and its associated glob patterns:
Use Cases¶
Discovering Types
Use --type-list | grep to find the correct type name for your language instead of guessing. This ensures your -t filters work correctly.
Discover Available Types:
Verify Custom Types:
Reference for Type Filters:
Custom Type Definitions¶
Add custom types and verify them:
# Add custom type
rg --type-add 'config:*.{yml,yaml,toml,json}' --type-list | grep config
# Use custom type
rg --type-add 'config:*.{yml,yaml,toml}' -t config 'database_url'
Custom Type Verification
The --type-list output reflects any custom types added via --type-add and respects --type-clear if used to remove built-in types. This makes it useful for verifying your type configuration:
Generation Mode¶
The --generate flag creates shell completions and man pages for ripgrep. All generated output is written to stdout, allowing you to pipe or redirect it as needed.
Supported Shells¶
ripgrep can generate completions for:
- Bash (
complete-bash) - Zsh (
complete-zsh) - Fish (
complete-fish) - PowerShell (
complete-powershell) - Man pages (
man)
Source: crates/core/flags/defs.rs:2454-2458, crates/core/main.rs:359-365
Generating Shell Completions¶
Generating Man Pages¶
# Generate man page
rg --generate man
# Install system-wide
rg --generate man | sudo tee /usr/local/share/man/man1/rg.1
# View generated man page
rg --generate man | man -l -
Installation Workflow¶
Complete Shell Integration
Complete shell integration setup for Zsh:
Version Information¶
Check ripgrep version and feature availability.
Version Flag¶
Example output from rg --version:
- Version number of the ripgrep binary
- Features available at compile time (prefixed with + or -)
- CPU features detected at runtime on your system
Version Flag Usage
The --version flag provides more verbose output including feature information, while -V provides a more compact version string. Use --version when you need to check feature support, and -V when you only need the version number.
PCRE2 Version Check¶
Verify PCRE2 support and JIT compilation:
Example output:
This confirms: - PCRE2 library version - Whether JIT (Just-In-Time) compilation is enabled - Feature availability for advanced regex patterns
Source: crates/core/main.rs:392-399
Use Cases¶
Check Before Using PCRE2
Always verify PCRE2 is available before using advanced regex features in scripts. This prevents runtime errors on systems without PCRE2 support.
Check Feature Availability:
# Verify PCRE2 before using advanced patterns
if rg --pcre2-version &>/dev/null; then
rg -P 'pattern(?=lookahead)'
else
echo "PCRE2 not available"
fi
Version-Specific Behavior:
# Check version for compatibility
version=$(rg --version | head -n1 | awk '{print $2}')
echo "Using ripgrep version: $version"
Practical Examples¶
Example 1: Audit Project Structure¶
Get a quick overview of your codebase composition by counting files per language:
# Count files by type
echo "Rust files:" $(rg --files -t rust | wc -l) # (1)!
echo "Python files:" $(rg --files -t py | wc -l) # (2)!
echo "JavaScript files:" $(rg --files -t js | wc -l) # (3)!
echo "Total files:" $(rg --files | wc -l) # (4)!
- Count Rust source files (*.rs) using type filter
- Count Python files (.py, .pyw, *.pyi)
- Count JavaScript files (.js, .jsx, *.mjs)
- Count all searchable files respecting ignore rules
Example 2: Find Large Files¶
Identify large files that might need optimization or exclusion from searches:
- Pipeline: list searchable files → get sizes → sort by size (largest first) → show top 20
Example 3: Verify Search Coverage¶
Understand how many files are being filtered by ignore rules:
# Compare total files vs searched files
total=$(find . -type f | wc -l) # (1)!
searched=$(rg --files | wc -l) # (2)!
echo "Total files: $total"
echo "Searched files: $searched"
echo "Filtered: $((total - searched))" # (3)!
- Count all files in directory tree (no filtering)
- Count files ripgrep would search (respects .gitignore, etc.)
- Calculate how many files are being filtered out
Example 4: Generate File Inventory¶
Create a machine-readable inventory for build systems or analysis tools:
# Create JSON inventory of searchable files
rg --files | jq -R . | jq -s '{files: .}' > inventory.json # (1)!
- Convert file list to JSON array: read lines → wrap each as JSON string → collect into array → save
Example 5: Setup Development Environment¶
# Complete shell integration setup script
#!/bin/bash
# Install completions # (1)!
completion_dir="$HOME/.local/share/bash-completion/completions" # (2)!
mkdir -p "$completion_dir" # (3)!
rg --generate complete-bash > "$completion_dir/rg" # (4)!
# Install man page # (5)!
man_dir="$HOME/.local/share/man/man1" # (6)!
mkdir -p "$man_dir" # (7)!
rg --generate man > "$man_dir/rg.1" # (8)!
# Update man database # (9)!
mandb "$HOME/.local/share/man" # (10)!
echo "ripgrep shell integration installed"
- Set up shell completions for tab completion in bash
- Define user-local completion directory (no sudo required)
- Create completion directory if it doesn't exist
- Generate and save bash completions to the directory
- Set up man page for offline documentation access
- Define user-local man page directory (section 1 for user commands)
- Create man directory structure if needed
- Generate and save man page in roff format
- Update the man page index database
- Run mandb to make the new man page searchable with
man rg
Integration with Development Workflows¶
Editor Integration¶
Many editors use rg --files for file discovery:
# Vim/Neovim with fzf
" Use ripgrep for file finding
let $FZF_DEFAULT_COMMAND = 'rg --files'
# VS Code uses ripgrep by default
# Configure search exclusions in settings.json
CI/CD Pipelines¶
# Verify expected files exist
if ! rg --files | grep -q 'README.md'; then
echo "ERROR: README.md missing"
exit 1
fi
# Count TODOs in codebase
todo_count=$(rg 'TODO|FIXME' --count | awk -F: '{sum+=$2} END {print sum}')
echo "TODOs in codebase: $todo_count"
Build Scripts¶
# Generate file list for build system
rg --files -t rust | sed 's/^/src\//' > build/sources.txt
# Find all test files
rg --files -g '*_test.go' > test-files.txt
Best Practices¶
- Use
--filesto preview search scope before running expensive queries - Check
--type-listto discover available file types instead of guessing - Generate and install shell completions for better command-line experience
- Use
--pcre2-versionto verify feature availability in scripts - Combine
--fileswith other Unix tools for powerful file analysis - Install man pages locally for offline reference
Troubleshooting¶
Completions Not Working¶
Common Issue
Problem: Shell completions not activated after generation
Solutions:
- Verify completion file is in correct directory
- Check that completion system is enabled in shell
- Restart shell or source configuration file
- Verify file permissions (should be readable)
Files Mode Showing Unexpected Results¶
Unexpected File Count
Problem: --files shows more/fewer files than expected
Solutions:
- Check ignore files (
.gitignore,.ignore) - Verify
--hiddenor--no-ignoreflags if needed - Test with different type filters to isolate issue
PCRE2 Not Available¶
PCRE2 Missing
Problem: --pcre2-version returns error
Solutions:
- Verify ripgrep was compiled with PCRE2 support
- Check ripgrep installation method (some packages exclude PCRE2)
- Consider reinstalling from source with PCRE2 enabled
See Also¶
- Common Options - Frequently used flags
- Manual Filtering: File Types - File type filtering details
- Troubleshooting - General troubleshooting guide