I would like Vim to stop highlighting matches when entering Insert mode. And not to highlight them again until a new search is performed.
A first thought would be to add the following line to my .vimrc
.
autocmd InsertEnter * nohlsearch
But from :help :nohlsearch
we know that nohlsearch
does not work in automatic commands.
One alternative would be this line, which clears the last search pattern. Having nothing to match, Vim will have nothing to highlight.
autocmd InsertEnter * let @/ = ""
This stops the highlighting as intended. Nonetheless, it has side effects: because the last search pattern is cleared, commands such as n
or /<CR>
will not behave as intended.
So my question is: how can one stop highlighting when entering Insert mode in an efficient way and with no major side effects or no side effects at all?