range expressions can also reference lines that match a specified pattern. As with searching in other
contexts, /
is used for forward search, and ? is used for backwards searches. Use patterns
with the format:
[line]/{pattern}/
or
[line]?pattern?
where line specifies which line to start searching from (default the current line), and {pattern} is the pattern to search for. When specified, line can be an absolute number, or another expression that evaluates to a line number.
For example, the range of lines between the first line that matches /find/
and line 5
is given by:
:/find/,5
As expected, Step 2 indicates that we yanked 3 lines, and Step 3 indicates that the
selection started with the line matching /find/
.
Suppose we wanted to skip a few lines of text before starting the search. For example, if we are editing a markdown document that contains meta information at the top of the file we may want to skip that content then search only the body text. If we know that the meta information is 3 lines long, the following range expression could be used:
:4/find/,$
Rather than starting the search from a fixed line, we can specify the starting line as a
pattern as well. This allows us to search the buffer for a specific condition, then
search for the pattern that will start the range, then proceed to the end of the range.
For example, to start from the line matching /anything/
, then search for the first
line matching /obsessed/
and include lines until the first line matching /venerable/
we could use the following:
:/anything//obsessed/,/venerable/
You can also search backwards from the line containing the cursor. For example, if the
cursor is on line 4 and you want the range of lines
between the first preceding line that matches search
and the current line:
:?search?,.
and finally, the range of lines between the first preceding line that matches search
and the next
line that matches venerable
:
:?search?,/venerable/