16

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
  • 2
    @ChristianBrabandt Yes, this is a question and answer at the same time. Stack Exchange explicitly encourages this, and since I found this feature and plugin very useful, I thought I would share it. – DJMcMayhem Jul 12 '16 at 17:54
  • 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
20

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
6

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
3

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.

2

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.

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.