Throughout this article we have discussed that ripgrep proceeds through several steps each time it is called:
- Select files to be searched
- Apply the specified pattern(s) to each in the specified input(s)
- Format and return each line of output
The first part of this two-part chapter discussed the regular expression syntax that is used to define the patterns that will be used to select lines. In this second part, we discus the various command-line options that are available to fine-tune our searches.
ripgrep offers a wide range of options that allow us to fine-tune how it should apply the patterns that we search for. In many cases this can significantly simplify pattern definitions, while also allowing the same patterns to be used in different contexts by specifying command-line options.
Case Sensitivity
By default ripgrep searches are case-sensitive, but this can be enabled and disabled using the
--case-sensitive
and --ignore-case
flags. ripgrep also has a --smart-case
option
which, when enabled, implements slightly different logic. First, this option is only activated when
the search pattern contains at least one literal character. Next, ripgrep examines the literal
characters and executes a case-sensitive search if any of them are uppercase letters, otherwise it
executes a case-insensitive search.
Pattern Inversion
While most ripgrep search patterns define specific text that should be matched and returned, there
are cases where we want the opposite - we want to see any lines that do not match a pattern. While
this can be achieved by writing inverted versions of the patterns themselves, we can specify whether
ripgrep should return matching or non-matching lines by specifying one of the--invert-match
or
--no-invert-match
(the default) options. Note that inverting the match only applies to
line-by-line matching so that is not always the same as inverting the pattern itself, but is a
simple means to achieve the same result in a wide range of scenarios.
Pattern Interpretation
Sometimes we want to simply search for a specific sequence of characters, which may or may not
contain characters that have special meaning in regular expressions. We can always escape these
characters in the pattern itself, but ripgrep provides a convenient alternative. The
--fixed-strings
and --no-fixed-strings
options tell ripgrep whether it should treat the
specified pattern as a literal string or a regular expression.
We discussed in the syntax chapter that, by default ripgrep considers
patterns to be Unicode aware, which can be a powerful default, but might also cause some patterns to
match more content than those who are more accustomed to ASCII-focused regular expression engines
might expect. As you might expect, ripgrep has an option for that - simply pass the --unicode
or --no-unicode
options to specify whether the pattern should be interpreted as a Unicode-aware
(the default) or ASCII-based patterns.
Match Scope
ripgrep offers a few different options that allow the scope of the search to be specified. When
the --word-regxp
option is used, ripgrep attempts to match the specified pattern within word
boundaries. Generally-speaking, ripgrep effectively splits each line of input into words, matches
the specified pattern against each word, then returns lines for which any word matched the pattern.
On the other hand, when the --line-regexp
is used the pattern is applied across each
full line of input.
ripgrep allows pattern matches to cross line boundaries, which can be enabled and disabled
with the --multiline
and --no-multiline
options.
When multi-line searches are enabled patterns are allowed to match newline characters (which is not
allowed by default) and continue matching the next line of input. One caveat is that the .
pattern
still matches anything but a newline character, although this can be customized by additionally
passing one of the --multiline-dotall
or --no-multiline-dotall
options.
Search Scope
By default ripgrep will match the specified pattern against each line of input, and continue matching through the entirety of each file. This behavior can be modified in case where that isn't the desired behavior.
Passing the --stop-on-nonmatch
instructs ripgrep to stop searching files when it
encounters the first non-matching line. Alternatively, the --max-count
option allows the number of
matches to be defined so that, for example, ripgrep matches up to a specific number of lines
before returning.