I've set hlsearch.

When I type :normal! n, the target text is highlighted.

But when I put normal! n in a function(Search) and call that function(:call Search()), the highlight doesn't take effect? How could I solve this problem? Any help would be appreciated!

function! Search()
    normal! n

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.