Run: nohlsearch in InsertEnter

I can do <C-O> :noh <CR>it when I enter insert mode, but it does not work when it is executed automatically:

autocmd InsertEnter * :nohlsearch

This works, but it behaves differently:

autocmd InsertEnter * :set nohlsearch

To clarify, I want to start :nohlsearchif I enter the insert node, but I still want to keep the ability to do / <CR> Nto search for another element.

+4
source share
2 answers

You must write a function that calls :nohl, and then redraw:

function DisableHL()
  nohl
  redraw
endfunction

and then autocmd InsertEnter * :call DisableHL()

+1
source

I think what you want can be achieved by directly setting the search register:

:autocmd InsertEnter * :let let @/=''

, , - :

:autocmd InsertEnter * :let b:_search=@/|let @/=''
:autocmd InsertLeave * :let @/=get(b:,'_search','')

b: _search.

+1

Source: https://habr.com/ru/post/1610491/


All Articles