I know that I can run a search in vim using /. I prefer having search highlighting turned on, but if I have a two splits with different files open, and I search for a term in one, they get highlighted as if I ran the same search or subsitution in both. Is there any way to disable this? The same thing occurs with tabs as well.

  • 3
    not possible. hlsearch will always highlight all windows. There is an open issue/todo item about changing it to a local option. I remember having looked into it and it wasn't that easy. Commented Nov 17, 2019 at 16:32

1 Answer 1


As far as I know, this isn’t possible. However, I found this neat mini-plugin in Paul Irish’s vimrc which does something close to what you’re looking for. As it stands, it highlights occurrences of the current word but I’m sure it could be updated to highlight a given string. Just add it to your vimrc and hit 1-6 to highlight the word under cursor in various colors.

  " This mini-plugin provides a few mappings for highlighting words temporarily.
  " Sometimes you're looking at a hairy piece of code and would like a certain
  " word or two to stand out temporarily.  You can search for it, but that only
  " gives you one color of highlighting.  Now you can use <leader>N where N is
  " a number from 1-6 to highlight the current word in a specific color.

  " credit: https://github.com/paulirish/dotfiles/blob/master/.vimrc

  function! HiInterestingWord(n) " {{{
    " Save our location.
    normal! mz

    " Yank the current word into the z register.
    normal! "zyiw

    " Calculate an arbitrary match ID.  Hopefully nothing else is using it.
    let mid = 86750 + a:n

    " Clear existing matches, but don't worry if they don't exist.
    silent! call matchdelete(mid)

    " Construct a literal pattern that has to match at boundaries.
    let pat = '\V\<' . escape(@z, '\') . '\>'

    " Actually match the words.
    call matchadd("InterestingWord" . a:n, pat, 1, mid)

    " Move back to our original location.
    normal! `z
  endfunction " }}}

  " Mappings {{{
  nnoremap <silent> <leader>1 :call HiInterestingWord(1)<cr>
  nnoremap <silent> <leader>2 :call HiInterestingWord(2)<cr>
  nnoremap <silent> <leader>3 :call HiInterestingWord(3)<cr>
  nnoremap <silent> <leader>4 :call HiInterestingWord(4)<cr>
  nnoremap <silent> <leader>5 :call HiInterestingWord(5)<cr>
  nnoremap <silent> <leader>6 :call HiInterestingWord(6)<cr>
  • 1
    Welcome to Vi and Vim! It might be useful to include a link to where you got this from. Also, note that the "id" for matchadd() is optional (and matchadd returns the ID for removing it later), so you can establish the highlight and clear it. Lastly, why the autocmd dance at the top? It never closes (augroup END) or sets up actual auto commands...
    – D. Ben Knoble
    Commented Nov 22, 2019 at 1:57
  • Thanks for the feedback, first post so definitely appreciated! Yes, you can clear highlight later using the same command <leader># which is useful. The original source is in the code as comment: github.com/paulirish/dotfiles/blob/master/.vimrc, I added to the answer to clarify. As for augroup, good question. I never even noticed that was there to be honest, don’t think there’s a good reason for it (at least not that I can think of). I mis-copied and left off the augroup end, which should be on the last line.
    – sfrieds3
    Commented Nov 22, 2019 at 2:06
  • Seems like it could be useful to either edit in the missing code, or remove it if it's not needed.
    – D. Ben Knoble
    Commented Nov 22, 2019 at 3:48
  • 1
    (Based on a glance through his vimrc, he really doesn't grok the purpose of autcommands, or he's doing something really bizarre. Not a pattern I'd copy, especially if any one is new to vim.)
    – D. Ben Knoble
    Commented Nov 22, 2019 at 3:54
  • Good point, I’ll edit the posted code. To be honest, I pulled this snippet out back in the day when I was new to vim and didn’t really get what was going on. It’s worked for me ever since so I haven’t had a reason to even look at this code in a few years.
    – sfrieds3
    Commented Nov 22, 2019 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.