I am trying to use a vim function to search whether
:noh has been set or not
This is similar to question found here except that question does not take
:noh into account
One way to search from a function is to use
let @/ = 'needle'
However, after I use
:noh, this no longer works for any of my functions. I have to call
:set hlsearch outside of my function for the search to work. Example
function! Needle() let @/ = 'needle' endfunction nnoremap try1 :call Needle()<return> nnoremap try2 :call Needle()<return>:set hlsearch<return>
the mapping "try1" does not work. The mapping "try2" does work.
Question - is there any way to get
:set hlsearch to work from the function?
update - I found that search is indeed being performed, but the highlight is removed. This can be undone by pressing
n in normal mode. It's still not executed from the funciton, but is closer to the solution I am looking for.
function! Needle() let @/ = 'needle' endfunction nnoremap try1 :call Needle()<return> nnoremap try2 :call Needle()<return>n
"try1" will not work but "try2" will
:h :noh, highlighting state is saved and restored when executing user functions. Based on that: not possible.
return ":let v:hlsearch = v:true\<CR>"worked. Although to be honest, I need to do some research on the
return ":let v:hlsearch = v:true\<CR>"part because I don't understand what that is doing and I don't like having code I don't understand
<expr>. That says the the right hand side of the mapping will be evaluated as a Vim expression (normally it's interpreted as is ... used literally) and the result will be the actual mapped action. So that makes sense here...the return value of the function (a string) is run as a command and the effect is to do
set hlsearchoutside the function.