Is there any way to have vim highlight the search with / and ? but not when putting cursor on a word and pressing * or #?

:set hlsearch and :set nohlsearch turn it on and off for both.


I don't think there's anything better than switching highlight off just after the search:

set hlsearch
nnoremap <silent>* *:noh<CR>
nnoremap <silent># #:noh<CR>
  • Thanks @Matt, this works, but it flashes the highlighting right after pressing * or #. Is there a way that doesn't do this? May 9 '20 at 14:29
  • 1
    @ThaddaeusMarkle Try set lazyredraw if you still don't have it. But there's no guarantee it helps.
    – Matt
    May 9 '20 at 14:39
  • thanks, that works! May 10 '20 at 16:59
  • A problem I've been facing with this is that set lazyredraw causes lots of lag, especially after vim has been open for a long time. May 28 '20 at 22:52
  • @ThaddaeusMarkle I doubt I can help you. Personally, I simply set incsearch honlsearch and don't mess with it. You can try to raise an issue on Vim's github page.
    – Matt
    May 29 '20 at 4:42

Maybe you want to try this :

  1. Open ~/.vimrc (create it if it didn't exist).
  2. Add "set hlsearch" in the file.
  3. Save the file.

Hope this will help.

  • Welcome to Vi and Vim! Unfortunately this doesn't really answer the question, if you re-read it you'll see the OP is already familiar with hlsearch but wants to temporarily disable while using the * and # commands that search for the word under the cursor...
    – filbranden
    May 8 '20 at 11:15
  • ahh, i see. that means I misunderstood this topic..
    – YonzLeon
    May 8 '20 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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