Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn on search highlighting from a vim script?

Tags:

vim

If I do either of the following two:

call search("searchString")

exec "/ searchString"

From a script, then vim does the search but does not highlight the results, even though hlsearch. Doing the same searches from outside a script highlights the results.

like image 248
crelbor Avatar asked Nov 26 '09 13:11

crelbor


People also ask

How do I enable highlight search in vim?

Vim's hlsearch option is a commonly-used way to enable visual feedback when searching for patterns in a Vim buffer. When highlighting of search matches is enabled (via :set hlsearch), Vim will add a colored background to all text matching the current search.

How do I change highlight color in vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.


2 Answers

Just found out the answer myself:

call search(l:searchString)
call matchadd('Search', l:searchString)
like image 186
crelbor Avatar answered Sep 27 '22 18:09

crelbor


The

feedkeys()

function is the key (pun intended):

call feedkeys("/pattern\<CR>")

or cleaner:

" highlights – or doesn’t – according to 'hlsearch' option
function SearcH(pattern)
    let @/ = a:pattern
    call feedkeys("/\<CR>")
endfunction 
like image 32
Aaron Thoma Avatar answered Sep 27 '22 20:09

Aaron Thoma