Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've set hlsearch on my .vimrc.

The problem is: when I find what I need in my document, I want it to automatically disable hlsearch when I enter insert mode for that searched string.

So, if I try to search for another string, it starts over again. This behavior is well known among other editors.

How do I do it?

share|improve this question

disable highlighting when entering insertmode via i:

noremap i :set nohlsearch<CR>i

to reenable it by leaving insert mode (via ESC):

inoremap <ESC> <ESC>:set hlsearch<CR>

you could apply these idea to all kind of keys like a, o etc.

OR

autocmd InsertEnter * :set nohlsearch
autocmd InsertLeave * :set hlsearch

(kudos for the 'InsertEnter' event idea)

share|improve this answer
    
Is there a Vim event that would be possible to add this config to avoid config to other keys? – Somebody still uses you MS-DOS Jun 24 '10 at 16:20
    
InsertEnter and InsertLeave are interesting. I'm going to check them out. ibm.com/developerworks/linux/library/l-vim-script-5/index.html – Somebody still uses you MS-DOS Jun 24 '10 at 16:49
    
true, good point. – akira Jun 24 '10 at 17:33
    
You probably also want to use 'setlocal' instead of ':set'. This will make it so that you dont modify the settings of other buffers. – Seamus Aug 16 '10 at 18:56
up vote 2 down vote accepted

I found a completely different approach, but that's is EXACTLY what I was looking for.

:set hlsearch
autocmd InsertEnter * :let @/=""
autocmd InsertLeave * :let @/=""

This sets the "latest search" register to the empty string, so that nothing will be highlighted. Earlier searches can still be remembered by using the key after hitting / or ? and optionally the "start" of the string to be searched.

Source

Thanks for your answer akira, but this solution is really elegant in my opinion. And it works, I tested it!

share|improve this answer
    
if it works, its fine with me. – akira Jun 28 '10 at 6:35
1  
This approach breaks the "search again" functionality (/ <cr>). – Xilexio Sep 21 '15 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .