Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/usr_27.txt`
975530168c9d973a0838b2fd403156c9d32cd30ac068ae8e0000000100000fa2
 the end of the file.  If it has
not found the string by that time, it starts from the beginning and searches
from the start of the file to the cursor location.
   Keep in mind that when repeating the "n" command to search for the next
match, you eventually get back to the first match.  If you don't notice this
you keep searching forever!  To give you a hint, Vim displays this message:

	search hit BOTTOM, continuing at TOP ~

If you use the "?" command, to search in the other direction, you get this
message:

	search hit TOP, continuing at BOTTOM ~

Still, you don't know when you are back at the first match.  One way to see
this is by switching on the 'ruler' option: >

	:set ruler

Vim will display the cursor position in the lower righthand corner of the
window (in the status line if there is one).  It looks like this:

	101,29       84% ~

The first number is the line number of the cursor.  Remember the line number
where you started, so that you can check if you passed this position again.


NOT WRAPPING

To turn off search wrapping, use the following command: >

	:set nowrapscan

Now when the search hits the end of the file, an error message displays:

	E385: search hit BOTTOM without match for: forever ~

Thus you can find all matches by going to the start of the file with "gg" and
keep searching until you see this message.
   If you search in the other direction, using "?", you get:

	E384: search hit TOP without match for: forever ~

==============================================================================
*27.3*	Offsets

By default, the search command leaves the cursor positioned on the beginning
of the pattern.  You can tell Vim to leave it some other place by specifying
an offset.  For the forward search command "/", the offset is specified by
appending a slash (/) and the offset: >

	/default/2

This command searches for the pattern "default" and then moves to the
beginning of the second line past the pattern.  Using this command on the
paragraph above, Vim finds the word "default" in the first line.  Then the
cursor is moved two lines down and lands on "an offset".

If the offset is a simple number, the cursor will be placed at the beginning
of the line that many lines from the match.  The offset number can be positive
or negative.  If it is positive, the cursor moves down that many lines; if
negative, it moves up.


CHARACTER OFFSETS

The "e" offset indicates an offset from the end of the match.  It moves the
cursor onto the last character of the match.  The command: >

	/const/e

puts the cursor on the "t" of "const".
   From that position, adding a number moves forward that many characters.
This command moves to the character just after the match: >

	/const/e+1

A positive number moves the cursor to the right, a negative number moves it to
the left.  For example: >

	/const/e-1

moves the cursor to the "s" of "const".

If the offset begins with "b", the cursor moves to the beginning of the
pattern.  That's not very useful, 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:

Title: Vim Search: Wrapping, Offsets, and Searching Backwards
Summary
This section explains how to disable search wrapping in Vim, which prevents searches from looping back to the beginning of the file. It then describes how to use offsets to position the cursor at specific locations relative to the search match, including character and line offsets. Finally, it covers how to repeat searches with different offsets and how to apply offsets when searching backwards using the '?' command, also it mentions how search starts normally at cursor position.