Reman
Reman

Reputation: 8119

Highlighting and functions

Let say I have this function in menu.vim:

  function! s:Test()
   let search = '\d\+'
   let @/ = search
   return "normal n" 
  endfunction

Why doesn't this highlight the search matches?
Only after I do :set hls in the commandline it does highlighting.
It does also highlighting when I push the n key on my keyboard.
If I put the same line (:set hls) in the function it doesn't work.
BTW highlighting is enabled in my VIM.
It highlights fine if I use the commandline.

Upvotes: 1

Views: 91

Answers (1)

Jörn Horstmann
Jörn Horstmann

Reputation: 34044

That behaviour might actually be described in the manual under :help functions-search-undo, but there is no mention of any workaround.

The last used search pattern and the redo command "." will not be changed by the function. This also implies that the effect of :nohlsearch is undone when the function returns.

The documentation on :nohlsearch also mention this:

This command doesn't work in an autocommand, because the highlighting state is saved and restored when executing autocommands autocmd-searchpat. Same thing for when invoking a user function.

Upvotes: 1

Related Questions