Output Formatting¶
These flags control what information is displayed with each match.
flowchart TD
Start[Search Match Found] --> Format{"Output
Format?"}
Format -->|Default| Auto[Auto-detect Context]
Format -->|--pretty| Pretty[Color + Heading + Line Numbers]
Format -->|Custom| Custom[Apply Custom Flags]
Auto --> MultiFile{"Multiple
Files?"}
MultiFile -->|Yes| ShowFile[Show Filename]
MultiFile -->|No| HideFile[Hide Filename]
ShowFile --> LineNum
HideFile --> LineNum
LineNum{Line Numbers?}
LineNum -->|-n| AddLine[Add Line Number]
LineNum -->|-N| NoLine[No Line Number]
AddLine --> Context
NoLine --> Context
Context{Context Lines?}
Context -->|None| Display[Display Match]
Context -->|-A NUM| After[Show NUM After]
Context -->|-B NUM| Before[Show NUM Before]
Context -->|-C NUM| Both[Show NUM Both]
After --> Display
Before --> Display
Both --> Display
Pretty --> Display
Custom --> Display
Display --> Color{Colorize?}
Color -->|--color always| Colored[Colored Output]
Color -->|--color never| Plain[Plain Output]
Color -->|--color auto| Terminal{To Terminal?}
Terminal -->|Yes| Colored
Terminal -->|No| Plain
style Start fill:#e8f5e9
style Display fill:#e1f5ff
style Colored fill:#fff3e0
style Plain fill:#f5f5f5
Figure: Output formatting decision flow showing how ripgrep combines different formatting options.
Alternative Output Formats¶
Ripgrep offers specialized output formats optimized for different use cases:
Vim-Compatible Format¶
--vimgrep: Print results in vim quickfix format
Each match appears on its own line with the format path:line:column:text. If a line contains multiple matches, it will be printed multiple times (once per match).
!!! warning "Output Size Consideration"
Lines with many matches will be printed multiple times, which can lead to significantly larger output. For editor integrations, consider using --json instead for more efficient programmatic consumption.
!!! tip "Editor Integration"
This format is designed for vim's quickfix list (:copen). Many editors can parse this format:
JSON Lines Format¶
--json: Print results as JSON Lines for programmatic consumption
Emits one JSON object per line with five message types:
=== "begin" Indicates a file is being searched and contains at least one match:
=== "match" Contains match details including line number, column, and matched text:
{"type":"match","data":{
"path":{"text":"src/main.rs"},
"lines":{"text":"fn main() {\n"},
"line_number":1,
"absolute_offset":0,
"submatches":[{
"match":{"text":"main"},
"start":3,
"end":7
}]
}}
=== "context"
Context lines around matches (when using -A, -B, or -C):
{"type":"context","data":{
"path":{"text":"src/main.rs"},
"lines":{"text":" println!(\"Hello\");\n"},
"line_number":2,
"absolute_offset":12
}}
=== "end" Marks the end of results for a file:
=== "summary" Final aggregate statistics:
{"type":"summary","data":{
"elapsed_total":{"secs":0,"nanos":12345678},
"stats":{
"matched_lines":42,
"matches":56,
"searches":150,
"searches_with_match":8
}
}}
!!! note "Automatic Statistics"
The --stats flag is implicitly enabled with --json, ensuring the summary message includes search statistics.
!!! tip "Programmatic Processing" JSON Lines format is ideal for:
- **Editor integrations** - Parse structured data instead of text output
- **CI/CD pipelines** - Extract match counts and statistics
- **Search result analysis** - Process with `jq`, Python, or other tools
Example with `jq`:
```bash
# Extract all matched file paths
rg --json pattern | jq -r 'select(.type=="match") | .data.path.text' | sort -u
# Count total matches
rg --json pattern | jq -r 'select(.type=="summary") | .data.stats.matches'
```
Line Numbers and Filenames¶
-
-n, --line-number: Show line numbers (default when searching files) -
-N, --no-line-number: Hide line numbers -
-H, --with-filename: Show filenames (default when searching multiple files) -
-I, --no-filename: Hide filenames -
--column: Show column numbers of matches -
Adds column position after line number, useful for editor integrations and precise navigation
Field Separators¶
Customize the separators between output fields (filename, line number, column number, and text):
--field-match-separator SEPARATOR: Set separator for match lines
# Use tab separator for easier parsing
rg --field-match-separator $'\t' pattern
# Output: file.txt<TAB>42<TAB>matching line
# Use custom separator
rg --field-match-separator ' | ' pattern
# Output: file.txt | 42 | matching line
Default is : (colon). Useful for generating machine-readable output or avoiding conflicts when searching for patterns containing colons.
--field-context-separator SEPARATOR: Set separator for context lines
Default is - (hyphen). Helps distinguish context lines from actual matches in the output.
Programmatic Parsing
Use custom field separators when post-processing ripgrep output:
Context Lines¶
Show lines before and/or after each match to understand the surrounding code:
When to Use Context Lines
Context lines are especially useful when:
- Understanding function scope:
-A 5after finding a function definition to see its first few lines - Finding usage patterns:
-B 3 -A 3around API calls to see how they're typically used - Debugging:
-C 10around error messages to see what triggered them - Code review:
-C 5to get enough context for understanding changes
-
-A NUM, --after-context NUM: Show NUM lines after each match -
-B NUM, --before-context NUM: Show NUM lines before each match -
-C NUM, --context NUM: Show NUM lines before AND after each match -
Shorthand for
-B 5 -A 5- shows the same number of lines before and after
Context Separators¶
When showing context lines, ripgrep prints a separator between different match groups to clearly distinguish them:
--context-separator SEPARATOR: Customize the separator string
# Use a custom separator
rg -C 2 --context-separator '━━━' pattern
# Use an empty line as separator
rg -C 3 --context-separator '' pattern
Default separator is --. You can specify any string, including empty strings or special characters.
--no-context-separator: Disable the separator entirely
Useful when you want continuous output without visual breaks, or when post-processing the results programmatically.
Visual Distinction
The default -- separator helps you quickly identify where one match context ends and another begins. Consider customizing it when:
- Output might naturally contain
--(e.g., searching code with command-line flags) - You need more visual separation with a longer separator like
═════ - Processing output programmatically and want a unique delimiter
Match Output¶
-
Useful for extracting specific data from files.-o, --only-matching: Print only the matched part of lines, not the full line -
-r, --replace REPLACEMENT: Replace matched text in output (doesn't modify files)
!!! warning "Output Preview Only"
The --replace flag only changes what ripgrep displays. It does not modify files on disk. To actually modify files, use tools like sed or your editor's find-and-replace. See the Replacements chapter for more details.
-
Useful for precise location tracking and binary file analysis.-b, --byte-offset: Show absolute byte offset in file for each match -
--hyperlink-format FORMAT: Generate clickable terminal links using OSC 8 escape sequences
!!! note "Terminal Support Required" Requires a terminal emulator that supports OSC 8 hyperlinks (e.g., iTerm2, kitty, WezTerm, Windows Terminal).
Built-in Format Aliases:
| Alias | Description | Format |
|---|---|---|
default |
RFC 8089 file:// scheme (platform-aware) | Platform-dependent |
none |
Disable hyperlinks | - |
cursor |
Cursor editor | cursor://file{path}:{line}:{column} |
file |
RFC 8089 file:// with host | file://{host}{path} |
grep+ |
grep+ scheme | grep+://{path}:{line} |
kitty |
kitty terminal with line anchor | file://{host}{path}#{line} |
macvim |
MacVim editor | mvim://open?url=file://... |
textmate |
TextMate editor | txmt://open?url=file://... |
vscode |
Visual Studio Code | vscode://file{path}:{line}:{column} |
vscode-insiders |
VS Code Insiders | vscode-insiders://file{path}:{line}:{column} |
vscodium |
VSCodium | vscodium://file{path}:{line}:{column} |
!!! tip "Custom Formats"
You can define custom formats using template variables: {path}, {line}, {column}, {host}. For example:
Color and Formatting¶
--color WHEN: Control colored outputauto(default): Color if outputting to terminalalways: Force color even when piping-
never: Disable color (useful for scripts) -
Use
-Rflag withlessto interpret color codes correctly -
Removes ANSI color codes from output file for clean text processing
-
--colors TYPE:STYLE:VALUE: Fine-grained color customization
Color Types:
path- File path in outputline- Line numberscolumn- Column numbersmatch- Matched text
Style Properties:
fg- Foreground (text) colorbg- Background colorstyle- Text style:bold,intense,underline,italic
!!! example "Color Customization Examples" === "Named Colors"
=== "256-Color Palette"
```bash
# Use extended color palette
rg --colors 'match:fg:208' pattern
```
=== "RGB Colors"
```bash
# 24-bit RGB (R,G,B)
rg --colors 'match:fg:255,128,0' pattern
```
=== "Multiple Styles"
```bash
# Combine styles for different elements
rg --colors 'path:fg:blue' \
--colors 'path:style:bold' \
--colors 'line:fg:yellow' \
--colors 'match:fg:red' \
--colors 'match:style:intense' \
pattern
```
-
--heading/--no-heading: Control file grouping in outputWith# Group matches by file with filename as header rg --heading pattern # Inline format: path:line:match on each line rg --no-heading pattern--heading, matches are grouped under filenames. With--no-heading, every line shows the full path. -
-p, --pretty: Alias for--color always --heading --line-number -
Equivalent to
rg --color always --heading --line-number pattern
Quick Readable Output
Use -p when you want nicely formatted output for human reading, especially when piping to less:
Additional Output Options¶
Statistics¶
--stats: Print aggregate statistics about the search
Displays search summary including: - Number of matched lines - Number of files with matches - Number of files searched - Time elapsed
Example output:
!!! note "JSON Format Integration"
When using --json, statistics are automatically enabled and included in the summary message. This flag has no effect with --files, --files-with-matches, --files-without-match, or --count.
Null-Separated Output¶
-0, --null: Use NUL byte (\0) as separator instead of newline
Essential for shell scripting when filenames may contain spaces, newlines, or other special characters.
--null-data: Treat input as NUL-separated instead of line-separated
Useful when processing output from tools that use null separators.
Output Buffering¶
--line-buffered: Force line-by-line output buffering
Useful for real-time processing of search results in pipelines.
--block-buffered: Use block buffering for better performance
Default buffering mode that optimizes throughput at the cost of delayed output.
Column Width Limiting¶
-M NUM, --max-columns NUM: Don't print lines longer than NUM bytes
Lines exceeding this limit are silently skipped. Useful when searching code that may contain minified files or generated content with extremely long lines.
--max-columns-preview: Print a preview of long lines instead of omitting them
When a line exceeds the column limit, ripgrep prints a preview instead of skipping it entirely, helping you identify what was truncated.
!!! tip "Handling Minified Files" Combine with type exclusions for better control: