As you've already noted in your own answer, it's far more efficient to turn off highlighting with the :nohlsearch
command (which can be abbreviated to :noh
) rather than turning off the entire feature and then turning it back on the next time you search.
If you want to switch off highlighting every time you use i
to enter insert mode, you can create a simple mapping to do so:
:nnoremap i :noh<cr>i
It's left as an exercise for the reader to create similar mappings for other commands where they want highlighting switched off.
More recent versions of Vim have an InsertEnter
autocommand which you might think could be used to easily create a cleaner implementation of switching off highlighting, but unfortunately, the :nohlsearch
command has no effect within an autocommand. A possible workaround is instead to reset the search pattern when entering insert mode, but that obviously has side effects you may not desire:
augroup ClearSearch
autocmd!
autocmd InsertEnter * let @/ = ''
augroup END
There is, however, a better technique, which is to use feedkeys
from the autocommand. This is what the vim-cool plugin mentioned in Jon Parise's answer uses.