You could highlight by using
<cword> via an
autocommand on an event like
CursorHold. But I’m going to show an alternative method.
* searches forward for the word under the cursor
# searches backward for the word under the cursor
Both have variants (
g#) that don’t restrict the searches to word-boundaries.
N to navigate.
hlsearch, these searches will also highlight matches.
Why do this?
- It uses a vim search instead of arbitrary highlights, so you can actually navigate between matches
- It uses existing vim machinery and needs little-to-no setup
- The commands are good to learn anyway (and then you start to like
gd, among others)
This is does not work for “partial words”: if you visually-select part of a word and hit
*, you won’t get the result you want.
One workaround is one of the many visual star plugins.
Another is to simply yank
y and then search
/<C-r>"<CR> (vim notation: type slash
/ to search, then Control-R quote to paste the selected text, then hit enter).