Ripgrep¶
24 cards — 🟢 5 easy | 🟡 7 medium | 🔴 3 hard
🟢 Easy (5)¶
1. How does rg differ from grep in terms of recursion?
Show answer
rg searches recursively by default without needing a -r flag. It also automatically skips binary files and respects .gitignore rules.Remember: "rg = ripgrep = recursive grep, fast." It respects .gitignore by default and uses Rust regex engine.
Fun fact: ripgrep is often 2-10x faster than grep -r due to parallelism and gitignore-aware file skipping.
2. How do you list only the filenames that contain a match with rg?
Show answer
Use the -l flag: rg -l "pattern". This prints file paths with matches but not the matching content.Example: rg -i error matches "error", "Error", "ERROR". Add -w for whole-word matching: rg -iw error.
Remember: "-i = ignore case, -w = whole word, -l = files only."
3. What does rg's -S (smart case) flag do?
Show answer
Smart case makes the search case-insensitive unless the pattern contains an uppercase letter, in which case it becomes case-sensitive.Example: rg -t py "import os" searches only Python files. rg -T js skips JavaScript files.
Remember: "-t = type include, -T = type exclude." Use rg --type-list to see all types.
4. How do you show context lines around matches with rg?
Show answer
Use -A N (after), -B N (before), or -C N (both): rg -C 3 "panic" shows 3 lines before and after each match. Context lines are prefixed with - instead of : to distinguish them from match lines.5. What is the difference between --heading and --no-heading in rg?
Show answer
--heading (default for terminal) groups matches by filename with the filename printed once above its matches. --no-heading prefixes every line with the filename (default for pipes). Use --no-heading for consistent machine-parseable output.🟡 Medium (7)¶
1. How do you restrict rg to search only specific file types?
Show answer
Use the -t flag with a type name: rg -t py "import". Use rg --type-list to see all built-in types. Use -T to exclude a type.Example: rg -C 3 "panic" shows 3 lines before and after each match. -B for before only, -A for after only.
Remember: "-C = Context (both), -B = Before, -A = After."
2. How do you use glob patterns to filter files in rg?
Show answer
Use the -g flag: rg -g '*.conf' "listen" searches only .conf files. Use ! for exclusion: rg -g '!tests/' "func" excludes the tests directory.Remember: "rg skips .gitignore'd files and hidden dirs by default." Use --no-ignore to search everything, --hidden to include dotfiles.
Gotcha: If rg misses results, check if the file is gitignored or binary.
3. What do the stacking -u flags do in rg?
Show answer
-u includes .gitignore'd files, -uu also includes hidden (dot) files, -uuu also includes binary files. Each -u removes one layer of filtering.Example: rg --glob "*.log" "timeout" searches only .log files. rg --glob "!*.min.js" skips minified JS.
Remember: "--glob = file pattern filter. ! prefix = exclude."
4. Can rg perform text replacements in files?
Show answer
rg can preview replacements with --replace (rg "old" --replace "new") but does not modify files. Pipe rg -l through xargs sed for actual replacement.Example: rg -r '$1_NEW' '(\w+)_old' --passthru replaces old suffix with _NEW in output. For actual file modification, pipe to sed or use sd.
Remember: "rg -r = replace in OUTPUT, not in files."
5. What is the difference between --count and --count-matches in rg?
Show answer
--count (-c) prints the number of matching lines per file. --count-matches prints the number of individual matches per file (a line with two matches counts as 2 with --count-matches but 1 with --count).Remember: "rg -z searches compressed files." Supports gzip, bzip2, xz, lz4, and zstd.
Example: rg -z "ERROR" /var/log/*.gz searches rotated log files without decompressing.
6. What does --max-count do in rg, and when would you use it?
Show answer
--max-count N (-m N) stops searching a file after N matches. Useful for quickly confirming presence without reading entire large files: rg -m 1 "TODO" returns at most one match per file.7. What information does rg --stats provide after a search?
Show answer
--stats appends a summary showing: total matches, total lines matched, total files matched, total files searched, total bytes searched, and elapsed time. Useful for understanding search scope and performance.Example: echo "hello world" | rg "\w+" -o outputs each word on its own line. Useful for extracting patterns from piped input.
Remember: "-o = only matching part, not the full line."
🔴 Hard (3)¶
1. How do you configure default rg options via a config file?
Show answer
Set RIPGREP_CONFIG_PATH to point to a config file (e.g., ~/.ripgreprc). Each line is one flag, such as --smart-case or --glob=!.git/. Options apply to every rg invocation.Remember: "-l = list files with matches, -c = count matches per file." Useful for piping: rg -l TODO | xargs wc -l.
Example: rg -c "FIXME" shows how many FIXMEs per file.
2. When would you use rg's -P flag, and what does it enable?
Show answer
-P enables PCRE2 regex, which supports lookaheads, lookbehinds, and backreferences not available in the default Rust regex engine. Use it for advanced patterns at the cost of some speed.Example: rg -g "*.py" -g "!test_*" "def " — search Python files but skip test files. Multiple -g flags combine.
Gotcha: Glob patterns are relative to the search directory, not the current directory.
3. Why is rg significantly faster than grep on large repositories?