Reputation: 9248
I am trying to get the following to work inside a function.
let pat = 'set'
:execute "normal /" . pat . "\<CR>"
However, when I run it like this (literally copy paste into vim)
:function! BSearch(pat)
execute "normal /" . a:pat . "\<CR>"
endfunction
:call BSearch('tag')
It does a search for tag, but doesn't do any of the highlighting. "set" is still highlighted, and if I press n
it goes to "set", not "tag".
I am at my wits end -- it seems that inside the function execute just behaves differently.
Please help if you know a workaround.
I was able to write out my full command and have it work as I expected
command! -nargs=1 Ack2 execute "Ack " . <q-args> | execute "/".<q-args>
The ack.vim
extension didn't highlight the terms I searched for. Since I have configured my search to use perl regular expressions (and ag
uses perl regular expressions, which is what I use as my searcher), the terms are now not only highlighted, I can step through them using n
like I would any normal search (except it is now across my whole project obviously).
Thanks a ton!
Upvotes: 1
Views: 71
Reputation: 9465
Just look at this:
:h function-search-undo
Here is the whole paragraph:
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.
Maybe you can move your search into a user command, for example:
command! -nargs=1 BSearch execute "/".<q-args>
Upvotes: 1
Reputation: 10274
You don’t need normal
to execute
a /
search. For your case, this should suffice:
function! BSearch(pat)
execute "/" . a:pat
endfunction
Upvotes: 1