Highlighting a search term without moving the cursor
http://vim.wikia.com/wiki/VimTip1572
If you used this plugin, you can simply enable the functionality
\m
then use a NumPad key to assign a colour to that word everywhere without moving your cursor.
1
You can use many different highlights at once for many different words, or more complex search patterns if you wish.
skyblue’s answer shows the core of the idea of how to use the normal search highlighting to accomplish what you want.
I extended that technique by making a keybinding that works similarly to the *
command, but without actually moving to the next match.
My thanks goes to garyjohn for pointing out the expand()
equivalent to *
’s word selection (to avoid using *
itself and restoring the view). This simplifies the code enough so that it can go directly in the mapping (obviates using a function). I have also added a mapping that matches partial words (like g*
).
:" The leader defaults to backslash, so (by default) this
:" maps \* and \g* (see :help Leader).
:" These work like * and g*, but do not move the cursor and always set hls.
:map <Leader>* :let @/ = '\<'.expand('<cword>').'\>'\|set hlsearch<C-M>
:map <Leader>g* :let @/ = expand('<cword>')\|set hlsearch<C-M>