Jump to content

How to Use Ripgrep (rg): Examples and Usage Tips

From RidgeRun Developer Wiki

ripgrep: Fast Recursive Search Tool

ripgrep (rg) is a fast command-line search tool designed to recursively search for text patterns inside files. It scans directories and looks for matching strings within file contents, rather than file names. It combines the usability of grep with significantly better performance by respecting .gitignore, skipping binary files, and using efficient search algorithms. It is especially useful for searching large codebases quickly and integrating into command-line workflows.

Installation

Install using a package manager

# Debian/Ubuntu
sudo apt install ripgrep

# Fedora
sudo dnf install ripgrep

# Arch Linux
sudo pacman -S ripgrep

# macOS
brew install ripgrep

Basic Usage

The core idea behind rg is:

rg PATTERN [PATH]

Minimal example

rg TODO

Example output:

main.c:42:// TODO: fix this
utils.c:10:// TODO: refactor

Search in a specific directory

rg TODO src/

Performance Characteristics

- Skips ignored files by default (.gitignore)
- Avoids binary files
- Uses parallel search
- Much faster than traditional grep on large trees

Common Patterns

Case-insensitive search

rg -i error

Show line numbers explicitly

rg -n TODO

Search only specific file types

rg TODO -t c

Search specific file extensions

rg TODO -g "*.c"

Search including ignored files

rg TODO --no-ignore

Functionally the same as rg -u TODO

Control ignore rules

rg -u TODO    # include ignored files
rg -uu TODO   # include ignored + hidden files
rg -uuu TODO  # include everything (including binary)

Useful when searching repositories with sub-repositories, which are often excluded by gitignore rules.

Search exact string (no regex)

rg -F "TODO:"

Show surrounding context

rg -C 3 TODO

Expected behavior:

- Shows 3 lines before and after each match
- Useful for understanding code in context

Output Format

ripgrep output commonly follows this structure:

file
line:match

Example:

main.c
42:// TODO: fix this

This predictable format makes it easy to pipe into other tools.

Using ripgrep with Other Tools

Combine with fzf

rg TODO | fzf

Expected behavior:

- ripgrep generates matches
- fzf lets you interactively filter them

Preview matches interactively

rg TODO | fzf --preview 'echo {}'

Expected behavior:

- The selected match is shown in the preview pane
- Useful when narrowing down large result sets

Open matches in an editor

vim "$(rg TODO | fzf | cut -d: -f1)"

Jump to line in file

selection=$(rg TODO | fzf)
vim "+$(echo "$selection" | cut -d: -f2)" "$(echo "$selection" | cut -d: -f1)"

Expected behavior:

- Select a match once
- The editor opens the correct file
- The cursor jumps to the matching line

Tips and Tricks

Prefer rg over grep -R

rg is usually faster and produces cleaner output

Limit search scope

rg TODO src/

Avoid searching entire repositories unnecessarily.

Use fixed-string search when possible

rg -F "ERROR"

This avoids regex overhead and improves speed.

Use file globs to narrow results

rg TODO -g "*.h"

This is useful when searching only certain file extensions.

Debug what is being searched

rg --debug TODO

Useful when results seem missing due to ignore rules.

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.