Terminal GuideTerminal Guide
ripgrep icon

ripgrep

Modern CLI
macOSLinuxWindows
Rust

Ultra-fast recursive grep alternative.

Official Website

Features

Ultra FastGitignore AwareUnicode SupportCompressed Files
Replaces
grep

Installation

Homebrew
brew install ripgrep
APT (Debian/Ubuntu)
apt install ripgrep
Pacman (Arch)
pacman -S ripgrep
Cargo (Rust)
cargo install ripgrep

Why Use ripgrep?

ripgrep (command name: rg) offers overwhelmingly faster speed than the traditional grep command and features optimized for modern development environments.

Overwhelming Speed

Optimized Rust code is 2-10x faster than GNU grep. Instant search even in large codebases.

.gitignore Auto-Recognition

Automatically recognizes .gitignore, .ignore, and .rgignore. Excludes node_modules and build artifacts, searching only needed files.

Full Unicode Support

Auto-detects character encodings like UTF-8 and UTF-16. Supports multilingual search including Japanese.

Compressed File Search

Search directly in compressed files (gz, bz2, xz, lz4) without extraction. Convenient for log analysis.

Basic Usage

Simple Search

Basic Commands
# Recursively search current directory and below
rg "search pattern"

# Search specific directory
rg "TODO" src/

# Display filenames only
rg -l "function"

# Case-insensitive search
rg -i "error"

Output Example

src/components/Button.tsx
15: const handleClick = () => {
23: onClick={handleClick}
src/hooks/useAuth.ts
8: export const handleClickEvent = () => {
42: handleClick is deprecated

File paths are purple, line numbers are green, and matched portions are highlighted.

Common Options

OptionDescriptionExample
-iCase insensitive searchrg -i "error"
-wMatch whole words onlyrg -w "log"
-lShow only matching file namesrg -l "TODO"
-cCount matchesrg -c "console"
-nShow line numbers (default)rg -n "func"
-A / -B / -CShow context lines (After/Before/Context)rg -C 3 "error"
-tSpecify file typerg -t py "import"
-gSpecify files with glob patternrg -g "*.ts" "type"
-vShow non-matching lines (invert)rg -v "debug"
--hiddenSearch hidden filesrg --hidden "secret"
-zSearch compressed filesrg -z "error" logs/
-UEnable multiline matchingrg -U "start.*end"

Regex Patterns

Using Regular Expressions

Regular Expressions
# Number patterns
rg "\d{3}-\d{4}"  # Phone number format

# Email address pattern
rg "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"

# Start and end of line matching
rg "^import"       # Lines starting with import
rg ";$"           # Lines ending with semicolon

# OR conditions
rg "error|warning|fatal"

# Any character
rg "log.+message"  # One or more characters between log and message

Literal Search (Disable Regex)

Literal Search
# -F option disables regex
rg -F "array[0]"   # Search special characters literally
rg -F "**/*.ts"    # Search glob pattern literally
rg -F "$HOME"      # String containing variable symbol

Multiline Search

Multiline Search
# -U option enables multiline matching
rg -U "function.*\{[\s\S]*?return" --multiline

# Search inside HTML tags
rg -U "<div class="error">[\s\S]*?</div>"

# Search multiline comment blocks
rg -U "/\*[\s\S]*?\*/"

Practical Examples

Code Search

Code Search
# Search function definitions in TypeScript files
rg -t ts "function\s+\w+"

# Search Python class definitions
rg -t py "^class\s+\w+"

# Find files containing specific imports
rg -l "from react" --type ts

# Find console.log for removal candidates
rg "console\.log" src/

# Search TODO/FIXME comments
rg "(TODO|FIXME|HACK|XXX):" --type-add 'code:*.{js,ts,py,go,rs}'

Context Lines Display

Context Display
# Show 3 lines before and after match
rg -C 3 "error"

# Show 5 lines after match
rg -A 5 "function main"

# Show 2 lines before match
rg -B 2 "return"

# Show separator between files
rg --heading "pattern"

File Type Specification

File Types
# Check available file types
rg --type-list

# Search specific types only
rg -t js "async"        # JavaScript files
rg -t rust "fn main"    # Rust files
rg -t py "def "         # Python files
rg -t html "<script"    # HTML files

# Exclude specific types
rg -T js "config"       # Search non-JavaScript files

# Specify multiple types
rg -t ts -t js "import"

# Add custom type
rg --type-add 'web:*.{html,css,js}' -t web "class"

Replacement Feature

Replacement
# Replacement preview (no actual changes)
rg "oldFunction" -r "newFunction"

# Replacement with capture groups
rg "(\w+)_controller" -r "$1Controller"

# Actual replacement combined with sed
rg -l "oldName" | xargs sed -i 's/oldName/newName/g'

# Display file contents while confirming
rg "pattern" -r "replacement" --passthru

fzf Integration

Interactive Search

fzf Integration
# Filter ripgrep results with fzf
rg --line-number --no-heading . | fzf

# Select files with preview
rg -l "pattern" | fzf --preview 'bat --color=always {}'

# Open file from search results
rg -l "TODO" | fzf --preview 'rg --color=always "TODO" {}' | xargs -o vim

# Real-time search (no initial query)
: | fzf --bind 'change:reload:rg --line-number {q} || true' --ansi --disabled

Useful Function Definitions

~/.bashrc or ~/.zshrc
# Interactive grep using fzf
rgf() {
  rg --color=always --line-number --no-heading "$@" |
    fzf --ansi \
        --delimiter : \
        --preview 'bat --color=always {1} --highlight-line {2}' \
        --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
        --bind 'enter:become(vim {1} +{2})'
}

# Interactive search in current directory
rgi() {
  rg --color=always --line-number "" |
    fzf --ansi --query="$1" \
        --delimiter : \
        --preview 'bat --color=always {1} --highlight-line {2}'
}

Configuration and Customization

Configuration File

Specify a configuration file using ~/.ripgreprc or the environment variable RIPGREP_CONFIG_PATH.

~/.ripgreprc
# Smart case (case insensitive when pattern is all lowercase)
--smart-case

# Search hidden files
--hidden

# Always exclude certain directories
--glob=!.git/
--glob=!node_modules/
--glob=!vendor/
--glob=!dist/
--glob=!build/
--glob=!*.min.js

# Maximum column width setting
--max-columns=150

# Always enable color output
--color=auto

# Show line numbers
--line-number

# Display filename as header
--heading

# Set match highlight colors
--colors=line:fg:yellow
--colors=match:bg:yellow
--colors=match:fg:black

Environment Variable Setup

~/.bashrc or ~/.zshrc
# Specify config file path
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"

# Color customization
export RIPGREP_CONFIG_PATH="$HOME/.config/ripgrep/config"

Project-Specific Settings (.rgignore)

.rgignore
# Place in project root
# Same format as .gitignore

# Build artifacts
dist/
build/
out/

# Dependencies
node_modules/
vendor/

# Log files
*.log
logs/

# Temporary files
*.tmp
*.temp
.cache/

# Test coverage
coverage/

Shell Aliases

Alias Configuration
# ~/.bashrc or ~/.zshrc

# Replace grep with rg
alias grep='rg'

# Common option combinations
alias rgg='rg --hidden --glob "!.git"'  # Search hidden files (exclude .git)
alias rgf='rg --files-with-matches'      # File names only
alias rgc='rg --count'                   # Count only
alias rgi='rg --ignore-case'             # Case insensitive

# Search with context
alias rg3='rg -C 3'                      # Show 3 lines before/after
alias rg5='rg -C 5'                      # Show 5 lines before/after

Comparison with grep

Featuregrepripgrep
Search SpeedStandard2-10x faster
Recursive SearchRequires grep -rRecursive by default
.gitignore Support-Auto-detection
Unicode SupportLimitedFull support
Compressed File SearchUse zgrep-z option
Color OutputRequires --colorEnabled by default
File Type Specification--include patternEasy with -t
Multiline MatchRequires -P (PCRE)Supported with -U
Replacement Preview--r option
PortabilityStandard on most systemsRequires separate installation

Benchmark Example (Linux Kernel Source Search)

GNU grep: ~5.2s
ripgrep: ~0.5s
* Results vary depending on environment and pattern

Tips

  • Use rg --type-list to see all supported file types. Over 200 types are supported.
  • Setting --smart-case enables smart mode: lowercase-only patterns are case insensitive, while patterns with uppercase characters are case sensitive.
  • Use --no-ignore to ignore .gitignore, or --no-ignore-vcs to ignore only VCS-related ignore files.
  • Vim users can replace the :grep command with rg for a better experience. Add set grepprg=rg\ --vimgrep to your .vimrc.
  • The --json option outputs in JSON format, useful for integration with other tools.
  • When searching large files, you can limit file size with --max-filesize 1M.
  • Binary files are automatically skipped. Use the --binary option if you want to search them.
Written by Dai AokiPublished: 2026-01-20

Related Articles

Explore More