The search highlight status is actually isolated from a function call, so you can't really re-enable it (or disable it) from inside a function.
See :help function-search-undo
:
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.
And :help :nohlsearch
includes:
This command doesn't work in an autocommand, because the highlighting state is saved and restored when executing autocommands. Same thing for when invoking a user function.
While the help mostly covers clearing the highlighting (with the :nohlsearch
command), the same is true for turning highlighting on when using a search command or the n
normal mode command.
Your best bet is to try to move the search command out of the function so it happens in a context where search highlighting will not be undone. For example, if you're calling the function from a mapping, consider using an <expr>
mapping and returning "n"
from the mapping, so that it actually executes outside of the function context and isn't affected by this behavior.