Taken from https://www.philipdaniels.com/blog/2019/ripgrep-cheatsheet/: ``` rg --help | more Make help useful on Windows rg -B NUM NEEDLE Show NUM lines before the match rg -A NUM NEEDLE Show NUM lines after the match rg -C NUM NEEDLE Show NUM lines before and after the match rg -l NEEDLE List matching files only rg -c NEEDLE List matching files, including a count rg -i NEEDLE Search case-insensitively rg --no-filename NEEDLE Don’t print filenames, handy when you care about the match more than the file rg -v NEEDLE Invert matching: show lines that do not match rg NEEDLE README.md Search only in specified file(s) rg -c --sort path | modified | accessed | created NEEDLE Sort the results (-sortr to reverse) rg -g '*.nuspec' NEEDLE Only search in *.nuspec files (can use multiple -g) rg -g '!*.nuspec' NEEDLE Search in everything but *.nuspec files rg -p NEEDLE | less -R Force pretty printed output even in pipes rg -e NEEDLE1 -e NEEDLE2 Search for multiple patterns rg -z NEEDLE Search in gzip, bzip2, xz, LZ4, LZMA, Brotli and Zstd compressed files rg --type-list Displays built-in available types and their corresponding globs rg -tcs -tconfig Search in file types cs and config rg -Tconfig Don’t search in file type config rg --files | rg NEEDLE Match against filenames rather than content ``` Note 1 If NEEDLE or -g patterns contain any special characters then place them in single quotes. Double quotes will work in some circumstances, but negative -g patterns in double quotes seem to confuse the shell, on Linux at least. Note 2 Remember that NEEDLE is a Regex, hence characters such as . (dot) have special meaning even when placed in single quotes. To match a literal . you need to use a Regex-escape: \. From https://docs.rs/regex/1.7.1/regex/#syntax: Matching one character ``` . any character except new line (includes new line with s flag) \d digit (\p{Nd}) \D not digit \pN One-letter name Unicode character class \p{Greek} Unicode character class (general category or script) \PN Negated one-letter name Unicode character class \P{Greek} negated Unicode character class (general category or script) ``` Character class ``` [xyz] A character class matching either x, y or z (union). [^xyz] A character class matching any character except x, y and z. [a-z] A character class matching any character in range a-z. [[:alpha:]] ASCII character class ([A-Za-z]) [[:^alpha:]] Negated ASCII character class ([^A-Za-z]) [x[^xyz]] Nested/grouping character class (matching any character except y and z) [a-y&&xyz] Intersection (matching x or y) [0-9&&[^4]] Subtraction using intersection and negation (matching 0-9 except 4) [0-9--4] Direct subtraction (matching 0-9 except 4) [a-g~~b-h] Symmetric difference (matching `a` and `h` only) [\[\]] Escaping in character classes (matching [ or ]) ``` Composites ``` xy concatenation (x followed by y) x|y alternation (x or y, prefer x) ``` Repetitions ``` x* zero or more of x (greedy) x+ one or more of x (greedy) x? zero or one of x (greedy) x*? zero or more of x (ungreedy/lazy) x+? one or more of x (ungreedy/lazy) x?? zero or one of x (ungreedy/lazy) x{n,m} at least n x and at most m x (greedy) x{n,} at least n x (greedy) x{n} exactly n x x{n,m}? at least n x and at most m x (ungreedy/lazy) x{n,}? at least n x (ungreedy/lazy) x{n}? exactly n x ``` Empty matches ``` ^ th beginnin of text (or start-of-line with multi-line mode) $ the end of text (or end-of-line with multi-line mode) \A only the beginning of text (even with multi-line mode enabled) \z only the end of text (even with multi-line mode enabled) \b a Unicode word boundary (\w on one side and \W, \A, or \z on other) \B not a Unicode word boundary ``` Groupings ``` (exp) numbered capture group (indexed by opening parenthesis) (?Pexp) named (also numbered) capture group (allowed chars: [_0-9a-zA-Z.\[\]]) (?:exp) non-capturing group (?flags) set flags within current group (?flags:exp) set flags for exp (non-capturing) ``` Flags ``` i case-insensitive: letters match both upper and lower case m multi-line mode: ^ and $ match begin/end of line s allow . to match \n U swap the meaning of x* and x*? u Unicode support (enabled by default) x ignore whitespace and allow line comments (starting with `#`) ```