This is done by setting just two options: set is hls
(incremental search and highlight all matches).
Make sure you've also read the appropriate help topics, excerpted below: :h 'is'
While typing a search command, show where the pattern, as it was typed
so far, matches. The matched string is highlighted. If the pattern
is invalid or not found, nothing is shown. The screen will be updated
often, this is only useful on fast terminals.
Note that the match will be shown, but the cursor will return to its
original position when no match is found and when pressing
Esc. You still need to finish the search command with
Enter to move the cursor to the match …
and :h 'hls'
.
When there is a previous search pattern, highlight all its matches.
The type of highlighting used can be set with the l
occasion in the highlight
option. This uses the
"Search" highlight group by default. Note that only the matching text
is highlighted, any offsets are not applied …
When you get bored looking at the highlighted matches, you can turn it
off with :nohlsearch
. As soon as you use a search command, the
highlighting comes back …
Since Patch 8.1.0271, incsearch
applies to :substitute
, :global
, and :vglobal
(also known as :s
, :g
, and :v
).
In Neovim incsearch
still does not affect any command, but there exists another option called :h 'inccommand'
. Currently it highlights all matches only for :h :substitute
. Besides this, it can also show a preview window with matched lines: :set inccommand=split
.