rg — Searching

Basic pattern search against /etc/passwd
rg 'root' /etc/passwd

Expected output:

1:root:x:0:0::/root:/bin/bash

rg prints line numbers by default, highlights matches in color. No flags needed for the common case.


Case-insensitive search (-i) across docs/
rg -i 'antora' ~/atelier/_bibliotheca/domus-captures/docs/

-i maps to grep -i. Searches recursively by default — no -r flag needed.


Word match (-w) — prevent partial matches
rg -w 'log' /etc/passwd

Without -w, log matches syslog, dialogd, catalog. With -w, only whole-word boundaries match. Equivalent to grep -w or wrapping the pattern in \b…​\b.


Fixed string / literal search (-F) — bypass regex engine
rg -F '$$' ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/pages/

-F treats the pattern as a literal string. Essential when searching for regex metacharacters ($, ., *, +, ?, [, {) without escaping each one.


Invert match (-v) — show non-matching lines
rg -v '^#' /etc/fstab

Prints every line that does NOT match the pattern. Here: all uncommented lines in fstab.


Count matches per file (-c)
rg -c 'include::' ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/pages/codex/grep/

Expected output:

basics.adoc:3
index.adoc:0
gotchas.adoc:2
pcre.adoc:2

Reports count per file. Files with 0 matches are shown too (unlike grep -c which omits them by default with -r).


First match only (-m 1) — early exit
rg -m 1 'nologin' /etc/passwd

Stops after the first match in each file. Useful for existence checks in large trees.


Multiline mode (-U) — match across line boundaries
cat <<'EOF' > /tmp/rg-multiline-test.txt
function setup() {
  echo "init"
}
EOF
rg -U 'function.*\{[\s\S]*?\}' /tmp/rg-multiline-test.txt

-U enables multiline mode. [\s\S]*? matches anything including newlines (non-greedy). Without -U, . never matches \n and patterns cannot span lines.


Quiet mode (-q) for scripting — exit code only
if rg -q 'PermitRootLogin' /etc/ssh/sshd_config; then
  echo "PermitRootLogin directive found"
else
  echo "PermitRootLogin directive missing"
fi

-q suppresses all output. Exit code 0 = match found, 1 = no match, 2 = error. Same semantics as grep -q.


Multiple patterns with -e — match any of several patterns
rg -e 'TODO' -e 'FIXME' -e 'HACK' ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/partials/

Each -e adds an alternative pattern. Lines matching ANY pattern are printed. Equivalent to grep -e 'A' -e 'B' or rg 'TODO|FIXME|HACK' — but -e is cleaner when patterns contain regex metacharacters.