11

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
crelbor
  • 521
  • 5
  • 15
  • I just found the answer myself.. always the way. Here it is: call search(l:searchString) call matchadd('Search', l:searchString) – crelbor Nov 26 '09 at 13:11
  • Please add your answer as a real answer here. It's hard to spot the comment when you are really looking for an answer. – innaM Nov 26 '09 at 13:13

7 Answers7

10

Just found out the answer myself:

call search(l:searchString)
call matchadd('Search', l:searchString)
crelbor
  • 521
  • 5
  • 15
  • 2
    Does this allow for the normal use of n/N for next/previous match? – alesplin Feb 12 '10 at 18:02
  • This really helps! – Life Jun 27 '16 at 20:39
  • 1
    @alesplin: [This](https://stackoverflow.com/questions/1803539/how-do-i-turn-on-search-highlighting-from-a-vim-script/47746577#14087567) answer below says no. I posted a [solution](https://stackoverflow.com/questions/1803539/how-do-i-turn-on-search-highlighting-from-a-vim-script/47746577#47746577). – Aaron Thoma Dec 11 '17 at 05:02
4

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 
Community
  • 1
  • 1
Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
  • This did work for me. It's similar as using the "n" button after a search. But I have a very very long search string. Is it possible to highlight matches without seeing the search string below? – Reman Feb 11 '19 at 10:46
1

I know this is late. However when I searched for the answer to this problem this page came up. So I feel compelled to help fix it.

call search(l:searchString)

call matchadd('Search', l:searchString)

Did not work for me. (when run from inside a function) It did higlight the words I wanted to search for but n/N wouldn't cycle between them. Also when I performed a new search the "l:serachStirng" pattern still remained highlighted. This answer on this link worked much better

Vim search and highlighting control from a script

Which gave me:

let @/ = l:searchString

then run

normal n

outside the funciton (so the highlighting is done immediately without the user needing to press n)

Community
  • 1
  • 1
Pev Hall
  • 11
  • 1
1

To turn on, press ESC type :set hls

To turn off, press ESC type :set nohls

Deanna
  • 23,876
  • 7
  • 71
  • 156
prakash j
  • 19
  • 1
0

Found answer here: http://vim.1045645.n5.nabble.com/highlighting-search-results-from-within-a-function-tt5709191.html#a5709193

```

One solution would be

function! XXXX() 
    execute '/this' 
    return @/ 
endfunction 

and to use the following instead of ":call XXXX()".

:let @/ = XXXX() 

```

vrybas
  • 1,719
  • 17
  • 12
0

I believe this works from inside a function (to just enable highlighting and nothing more):

call feedkeys(":\<C-u>set hlsearch \<enter>")

J. Doe
  • 73
  • 5
-3

You need to put this in your .vimrc file

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

The .vimrc file is usually located in your home directory, or you can find it using "locate .vimrc"

Martin Andersson
  • 1,801
  • 4
  • 21
  • 25
  • 1
    This is about scripting in vim, not turning on highlighting searches and syntax highlighting in general. – crelbor Nov 26 '09 at 13:10