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:nohlsearch:echo &hlsearchThis prints 1 (hlsearch is still set!):set hlsearch:set nohlsearch:echo &hlsearchThis 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.
What is the functional difference between :nohlsearch and :set nohlsearch?
search;highlight
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 hellos will be highlighted . If you use :nohlsearch nothing will be highlighted. But if you then search for world, all worlds 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()    nohlsearchendfunctionhas 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-variablev: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