I noticed that when I use :nohlsearch that it does not actually do the same thing as :set nohlsearch.

In particular, the hlsearch setting is not actually switched off (which is what :set hlsearch) accomplishes.

To demonstrate what I mean:

:set hlsearch
:echo &hlsearch

This prints 1 (hlsearch is still set!)

:set hlsearch
:set nohlsearch
:echo &hlsearch

This prints 0 because we switched hlsearch off using the standard way to switch a setting off in vim.

So I'm sort of wondering because there has to be some actual reason for the :nohlsearch ex command to exist.

The reason why I care is that I now have a need in my vim scripting to test whether the search highlight is active, and since I was using :nohlsearch instead of ":set nohlsearch" in my other scripting, my &hlsearch test always returns 1. So I am wondering what I'm changing by no longer using the nohlsearch ex command.

2 Answers 2


From :h :nohlsearch

                                                        :noh :nohlsearch
:noh[lsearch]           Stop the highlighting for the 'hlsearch' option.  It
                        is automatically turned back on when using a search
                        command, or setting the 'hlsearch' option.
                        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.

The key point is that it automatically turns the highlighting back on when you start a new search. :set nohlsearch actually turns hlsearch off, which means the next time you search for something no highlighting occurs.

Example: Assume hlsearch is on, If you are currently searching for "hello" in a buffer, all "hello"s will be highlighted . If you use :nohlsearch nothing will be highlighted. But if you then search for "world", all "world"s will be highlighted. If you had used set nohlsearch instead "world" would not be highlighted.

Also note that this command does basically nothing inside a user function. For example,

function! NoHlsearch()

has no visible effect when run.

To see if hlsearch is actually active you want to check the variable v:hlsearch. When v:hlsearch = 0 highlighting is off, and when v:hlsearch = 1 highlighting is on. You should probably make sure the &hlsearch = 1 before checking v:hlsearch.

From :h v:hlsearch

                                        v:hlsearch hlsearch-variable
v:hlsearch      Variable that determines whether search highlighting is on.
                Makes sense only if 'hlsearch' is enabled which requires
                +extra_search. Setting this variable to zero acts the like
                :nohlsearch command, setting it to one acts like
                        let &hlsearch = &hlsearch
  • Can you clarify what you mean by "basically nothing" from inside a function? It does do the standard thing (it temporarily stops highlighting the search until the next time search is invoked).
    – Steven Lu
    May 2, 2015 at 0:33
  • I'm also sort of hoping to find a way to access the flag that controls whether the transient :nohlsearch is in effect or not... I think in its absence I have to resort to tracking all the :nohlsearch calls with a variable.
    – Steven Lu
    May 2, 2015 at 0:37
  • @StevenLu see edits.
    – FDinoff
    May 2, 2015 at 0:41
  • Thanks. Looks like the code I have been using hacks around the "basically doing nothing" aspect of it by doing this: nnoremap <silent> <expr> <boundkey> SearchHighlight() where SearchHighlight() may return a string ":silent nohlsearch\<CR>". Also thank you for the v:hlsearch tip, just what I needed.
    – Steven Lu
    May 2, 2015 at 0:46

Does your script require that the current state of highlighting be known?

To turn off hlsearch for the current search only, I use Ctrl-l, with this in my .vimrc:

nnoremap <silent> <C-l> :<C-u>nohlsearch<CR><C-l>

Search highlighting returns with the next search when using the above command. Ctrl-l is convenient because it's normal use is to clear and redraw the screen.

To change the search highlighting state on or off persistently, I use F3 as my toggle:

nnoremap <F3> :set hlsearch! hlsearch?<CR>

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.