Replace, Preprocess, and Encodings

Preview replacements, search transformed files, and handle compressed or non-UTF8 data.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Replacement previews
Preview a simple replacement
rg --replace 'logger.error($1)' 'console\.error\((.*)\)' src/

# Show what matched lines would look like after substitution.

Use capture groups in replacements
rg --replace '$2_$1' '(\w+)-(\w+)' sample.txt

# Reuse matched text in the replacement output.

Print replaced fragments only
rg -o --replace '$1' 'name="([^"]+)"' template.html

# Combine `-o` and `--replace` to extract transformed matches.

Preview advanced regex replacements
rg -P --replace '$1' '(?<=id=)\d+' data.txt

# Use PCRE2 pattern features with replacement preview.

Preview and select candidates interactively
rg --line-number --no-heading 'TODO' . | fzf

# Pipe results into a fuzzy finder before editing.

## Preprocessors and compressed files
Search compressed files
rg -z 'Exception' logs/

# Enable searching in common compressed formats.

Use a preprocessor for unsupported formats
rg --pre 'pdftotext -layout' 'invoice' docs/*.pdf

# Transform file content before ripgrep searches it.

Apply preprocessing only to certain files
rg --pre 'pandoc -t plain' --pre-glob '*.docx' 'roadmap' docs/

# Limit preprocessors to matching paths.

Combine archive tools with rg
tar -xOf backup.tar config/app.yml | rg 'database:'

# Expand an archive stream and search it.

Search binary-ish files as text
rg -a 'sqlite' mystery.bin

# Treat binary files as text input.

## Encodings and text quirks
Search a UTF-16LE file
rg --encoding utf-16le 'Version' report.txt

# Decode a file using a specific encoding.

Search Shift_JIS text
rg --encoding shift_jis 'エラー' app.log

# Decode legacy encoded content before matching.

Disable line numbers for transformed output
rg -N --replace '$1' '"([^"]+)"' data.json

# Print cleaner replacement previews.

Print NUL-delimited paths
rg -l -0 'TODO' . | xargs -0 sed -n '1p'

# Use NUL separators to make filename parsing safe.

Recommended next

No recommendations yet.