"/TheLongFunctionName", but
that's a lot of typing. And when you make a mistake Vim won't find it.
There is an easier way: Position the cursor on the word and use the "*"
command. Vim will grab the word under the cursor and use it as the search
string.
The "#" command does the same in the other direction. You can prepend a
count: "3*" searches for the third occurrence of the word under the cursor.
SEARCHING FOR WHOLE WORDS
If you type "/the" it will also match "there". To only find words that end
in "the" use: >
/the\>
The "\>" item is a special marker that only matches at the end of a word.
Similarly "\<" only matches at the beginning of a word. Thus to search for
the word "the" only: >
/\<the\>
This does not match "there" or "soothe". Notice that the "*" and "#" commands
use these start-of-word and end-of-word markers to only find whole words (you
can use "g*" and "g#" to match partial words).
HIGHLIGHTING MATCHES
While editing a program you see a variable called "nr". You want to check
where it's used. You could move the cursor to "nr" and use the "*" command
and press "n" to go along all the matches.
Vim will highlight all matches. That is a very good way to see where the
variable is used, without the need to type commands.
To switch this off: >
:set nohlsearch
Then you need to switch it on again if you want to use it for the next search
command: >
:set hlsearch
If you only want to remove the highlighting, use this command: >
:nohlsearch
This doesn't reset the option. Instead, it disables the highlighting. As
soon as you execute a search command, the highlighting will be used again.
Also for the "n" and "N" commands.
TUNING SEARCHES
There are a few options that change how searching works. These are the
essential ones:
>
:set nowrapscan
This stops the search at the end of the file. Or, when you are searching
backwards, it stops the search at the start of the file. The 'wrapscan'
option is on by default, thus searching wraps around the end of the file.
>
:set noincsearch
This disables the display of the matches while you are still typing your
search.
INTERMEZZO
If you like one of the options mentioned before, and set it each time you use
Vim, you can put the command in your Vim startup file. Edit the file, for
example with: >
:edit ~/.config/nvim/init.vim
Then add a line with the command to set the option, just like you typed it in
Vim. Example: >
Go:set hlsearch<Esc>
"G" moves to the end of the file. "o" starts a new line, where you type the
":set" command. You end insert mode with <Esc>. Then write and close the
file: >
ZZ
If you now start Vim again, the 'hlsearch' option will already be set.
==============================================================================
*03.9* Simple search patterns
The Vim editor uses regular expressions to specify what to search for.
Regular expressions are an extremely powerful and compact way to specify a
search pattern. Unfortunately, this power comes at a price, because regular
expressions are a bit tricky to specify.
In this section we mention only a few essential ones. More about search
patterns and commands can be found in chapter 27 |usr_27.txt|. You can find
the full explanation here: |pattern|.
BEGINNING AND END OF A LINE
The ^ character matches the beginning of a line. On an English-US keyboard
you find it above the 6. The pattern "include" matches the word include
anywhere on the line. But the pattern "^include" matches the word include
only if it is at the beginning of a line.
The $ character matches the end of a line. Therefore, "was$" matches the
word was only if it is at the end of a line.
Let's mark the places where "/the" matches in this example line with "x"s:
the solder holding one of the chips melted and the ~
xxx xxx xxx
Using "/the$" we find this match:
the solder holding one of the chips melted and the ~
xxx
And with "/^the" we find this one:
the