19

I love the "hlsearch" setting, but after I'm done searching, I find it obnoxious. Especially if I search for something that has a lot of matches. I know that I can turn this off with

:set nohlsearch

But this takes a whole 16 keystrokes, and even more annoyingly, I have to turn it back on for my next search with

:set hlsearch    

Is there a way I can have hlsearch automatically toggle on when I start searching, and off when I am done searching? For example, if I enter insert mode, or modify the text, have hlsearch turn off without any additional keystrokes?

  • 3
    Define automatically. – Antony Jul 12 '16 at 17:16
  • @Antony As in, if I'm making edits, or in insert mode, it will turn off without me needing to press any buttons. If I start searching with / or ?, or if I press n or N, it will turn on without me needing to press any more buttons. – DJMcMayhem Jul 12 '16 at 17:18
  • 1
    Oh and define "done" searching – Christian Brabandt Jul 12 '16 at 17:53
  • 1
    :nno <BS> :set hls!\|set hls?<CR> -- one line, one keypress, state feedback, and doesn't try to second-guess when you may or may not want highlighting. Sometimes I want highlighting left on the screen when editing. Sometimes I don't, e.g. using / as a motion with an operator. – Antony Jul 12 '16 at 19:57
  • nnoremap <M-\> :set hlsearch!<CR><Bar>:echo 'hlsearch: ' . ['Off', 'On'][&hlsearch]<CR> – Sato Katsura Jul 13 '16 at 7:07
26

There is a much more convenient way. Rather than using

:set nohlsearch

which actually turns the hlsearch setting off, use

:nohls

This will only turn hlsearch off until you search again. From :help nohls

                 *'hlsearch'* *'hls'* *'nohlsearch'* *'nohls'*
'hlsearch' 'hls'    boolean (default off)
            global
            {not in Vi}
            {not available when compiled without the
            |+extra_search| feature}

    ...

    When you get bored looking at the highlighted matches, you can turn it
    off with |:nohlsearch|.  This does **not change the option value**, as
    soon as you use a search command, the highlighting comes back.

However, this solution isn't perfect, since you still have to type out :nohls which is pain to do after every search. You can get around this with a mapping, e.g.

nnoremap <esc><esc> :silent! nohls<cr>

But there is an even better solution! haya14busa/incsearch.vim. This is, at least in my opinion, an essential vim plugin. The main feature is that all search matches are highlighted in real time, as you are typing out your regex. For example:

enter image description here

However, a secondary feature that is also provided is the option to immediately toggle hlsearch on and off when you start/stop searching. To enable this feature, you will have to add

set hlsearch
let g:incsearch#auto_nohlsearch = 1
map n  <Plug>(incsearch-nohl-n)
map N  <Plug>(incsearch-nohl-N)
map *  <Plug>(incsearch-nohl-*)
map #  <Plug>(incsearch-nohl-#)
map g* <Plug>(incsearch-nohl-g*)
map g# <Plug>(incsearch-nohl-g#)

to your .vimrc.

If you have any other n mappings, you will have to turn these off. For example, I used to have

nnoremap n nzz
nnoremap N Nzz

These two mappings are not compatible with eachother!

  • Since you are doing a self answer i'd like to suggest that you also add another problem with set hlsearch. If you have it defined in vimrc it will overwrite :noh every time you source the vimrc. To sort this out I have if !&hlsearch | set hlsearch | endif in my vimrc. (And this is another problem that the plugin solves.) – grochmal Jul 12 '16 at 17:59
  • Dunno, seems like a whole heap of code and config for not a lot of gain to me. In fact it makes the target of your incremental search harder to spot, making it less useful for quick navigation. – Antony Jul 12 '16 at 19:48
  • +1 for a great plugin answering question and doing a lot more! – RichVel Feb 2 '17 at 16:04
7

vim-cool is a simple plugin that does just this.

Vim-cool disables search highlighting when you are done searching and re-enables it when you search again.

It works without remapping by using the CursorMoved and InsertEnter autocommands.

  • This technique (using autocommands) is way better than my answer, and I can't believe I didn't think of it when I wrote mine. Upvote. – Rich Aug 30 '17 at 12:35
5

I use <Leader><Enter> as a shortcut to :noh. (My leader is ,, so it's pretty close)

This will turn off the highlights until your next search.

3

As you've already noted in your own answer, it's far more efficient to turn off highlighting with the :nohlsearch command (which can be abbreviated to :noh) rather than turning off the entire feature and then turning it back on the next time you search.

If you want to switch off highlighting every time you use i to enter insert mode, you can create a simple mapping to do so:

:nnoremap i :noh<cr>i

It's left as an exercise for the reader to create similar mappings for other commands where they want highlighting switched off.

2

It might sound stupid or naive, but what I do when I want to stop the highlighting is I search for a random string that (virtually certainly) is not present in the document. Something like this:

/skljdfhadhfs

Granted, it does take a few key strokes, but they are really random, so they can be typed in no time.

  • 1
    But this makes n and N no longer work, and also things like :%s//something or :g//something. It also clutters your search history – DJMcMayhem Jul 3 '19 at 18:38
  • Yes, but my (probably incorrect) assumption is that since you don't need highlighting any more, you don't care about that particular search either. Cluttering the search history is a real issue, if one relies on it. – Kostas Jul 3 '19 at 18:40
1

I have found it more intuitive to remap the Ctrl-L repaint to include :nohls:

nnoremap <C-L> :nohls<cr><C-L>
  • Welcome to Vi and Vim! Thank you for sharing your knowledge on what is admittedly an old question. High quality answers, as you might have noticed, generally come with some explanation of what the code is doing (this is especially helpful for newer vimmers who might land here without much knowledge). It can also be a good idea to the relevant :help sections! – D. Ben Knoble Dec 19 '19 at 22:48
0

I use <Leader><Enter> as a shortcut to :hls and have my leader set as space.

let mapleader = "\<Space>"
nmap <silent> <leader><Esc> :set hls!<cr>

After searching an item will be highlighted, to turn it off, hit <Space><Esc>. And if you decide you want to redisplay the search results, hit <Space><Esc>. The hls! means map this key binding to toggle the setting for hls (shorthand for hlsearch, i.e. search result highlighting) (so the ! toggles the setting right before it).

-1

Add this macro to your .vimrc

map #h :set hls!<CR>

This will allow you to toggle hilighting with #h

  • This is no different than James Affleck's answer. – jamessan Nov 1 '19 at 22:49
  • Mine seems more concise. – Darrel Lee Nov 8 '19 at 19:46
  • Why is conciseness relevant? The other post gives more information about what the parts of the command mean and this post provides no additional content. – jamessan Nov 8 '19 at 19:53

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.