Home Explore Blog CI



neovim

3rd chunk of `runtime/doc/usr_27.txt`
934ac600cf32045c2e628b0f6a1758d073267dd0e89d40930000000100000fa5
 since leaving out the "b" does the same
thing.  It does get useful when a number is added or subtracted.  The cursor
then goes forward or backward that many characters.  For example: >

	/const/b+2

Moves the cursor to the beginning of the match and then two characters to the
right.  Thus it lands on the "n".


REPEATING

To repeat searching for the previously used search pattern, but with a
different offset, leave out the pattern: >

	/that
	//e

Is equal to: >

	/that/e

To repeat with the same offset: >

	/

"n" does the same thing.  To repeat while removing a previously used offset: >

	//


SEARCHING BACKWARDS

The "?" command uses offsets in the same way, but you must use "?" to separate
the offset from the pattern, instead of "/": >

	?const?e-2

The "b" and "e" keep their meaning, they don't change direction with the use
of "?".


START POSITION

When starting a search, it normally starts at the cursor position.  When you
specify a line offset, this can cause trouble.  For example: >

	/const/-2

This finds the next word "const" and then moves two lines up.  If you
use "n" to search again, Vim could start at the current position and find the
same "const" match.  Then using the offset again, you would be back where you
started.  You would be stuck!
   It could be worse: Suppose there is another match with "const" in the next
line.  Then repeating the forward search would find this match and move two
lines up.  Thus you would actually move the cursor back!

When you specify a character offset, Vim will compensate for this.  Thus the
search starts a few characters forward or backward, so that the same match
isn't found again.

==============================================================================
*27.4*	Matching multiple times

The "*" item specifies that the item before it can match any number of times.
Thus: >

	/a*

matches "a", "aa", "aaa", etc.  But also "" (the empty string), because zero
times is included.
   The "*" only applies to the item directly before it.  Thus "ab*" matches
"a", "ab", "abb", "abbb", etc.  To match a whole string multiple times, it
must be grouped into one item.  This is done by putting "\(" before it and
"\)" after it.  Thus this command: >

	/\(ab\)*

Matches: "ab", "abab", "ababab", etc.  And also "".

To avoid matching the empty string, use "\+".  This makes the previous item
match one or more times. >

	/ab\+

Matches "ab", "abb", "abbb", etc.  It does not match "a" when no "b" follows.

To match an optional item, use "\=".  Example: >

	/folders\=

Matches "folder" and "folders".


SPECIFIC COUNTS

To match a specific number of items use the form "\{n,m}".  "n" and "m" are
numbers.  The item before it will be matched "n" to "m" times |inclusive|.
Example: >

	/ab\{3,5}

matches "abbb", "abbbb" and "abbbbb".
  When "n" is omitted, it defaults to zero.  When "m" is omitted it defaults
to infinity.  When ",m" is omitted, it matches exactly "n" times.
Examples:

	pattern		match count ~
	\{,4}		0, 1, 2, 3 or 4
	\{3,}		3, 4, 5, etc.
	\{0,1}		0 or 1, same as \=
	\{0,}		0 or more, same as *
	\{1,}		1 or more, same as \+
	\{3}		3


MATCHING AS LITTLE AS POSSIBLE

The items so far match as many characters as they can find.  To match as few
as possible, use "\{-n,m}".  It works the same as "\{n,m}", except that the
minimal amount possible is used.
   For example, use: >

	/ab\{-1,3}

Will match "ab" in "abbb".  Actually, it will never match more than one b,
because there is no reason to match more.  It requires something else to force
it to match more than the lower limit.
   The same rules apply to removing "n" and "m".  It's even possible to remove
both of the numbers, resulting in "\{-}".  This matches the item before it
zero or more times, as few as possible.  The item by itself always matches
zero times.  It is useful when combined with something else.  Example: >

	/a.\{-}b

This matches "axb" in "axbxb".  If this pattern would be used: >

	/a.*b

It would try to match as many characters

Title: Repeating Searches, Start Position Compensation, and Advanced Matching in Vim
Summary
This section covers how to repeat searches with different offsets in Vim, and how Vim compensates for line offsets to prevent getting stuck in a loop. It also explains advanced matching techniques, including matching multiple times with '*', '+', and '=', specifying specific counts with '{n,m}', and matching as little as possible with '{-n,m}'.