Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

After a search I've done, all the matches for the respective query remain highlighted and it's annoying. This happens even after I close the respective file (normal behavior?). How to I remove this?

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

You want the command :nohlsearch, but you don't want to type that every time. Put this in your ~/.vimrc:

nnoremap <silent> <leader>n :nohlsearch<CR>

After that, you just enter your leader (which by default is \) and then n to clear the matches. You don't need to use n; if you prefer, pick something else.

You can also remap your leader to something more comfortable, if you like. I use a comma:

let mapleader = ","

(That goes before the mapping to clear searches.)

share|improve this answer
    
Thanks! It helped a lot. –  Alex Aug 9 '10 at 11:11
3  
I just do :noh which is enough for vim to know what I mean. :) –  sml Aug 9 '10 at 14:16
    
@scottl Fair enough (and good reminder that all commands have shortcuts), but I still like to map frequently used commands. –  Telemachus Aug 9 '10 at 14:25
add comment

i just /qiw772723euz to "search" for something thats not in the file. hacky workaround, i know, i am interested in a real solution as well :)

share|improve this answer
    
+1 for a nice hack :) –  Alex Aug 9 '10 at 11:11
    
thats awful but a nice quick and dirty 'solution' though :) –  matthias krull Aug 9 '10 at 13:59
add comment

Similar to @Telemachus answer above, a comment on this answer over at SO by @David Winslow suggested mapping a toggle of hlsearch. You would map the following:

nmap <silent> <leader>n :set hlsearch!<CR>

Hitting n would then toggle highlight search off and on every time you hit it.

share|improve this answer
add comment

In my config, I clear highlighted search terms by just hitting enter in command mode. This is how that's configured.

set hlsearch    " highlight all matched terms
" Pressing return clears highlighted search
:nnoremap <CR> :nohlsearch<CR>/<BS>

As a side note, I like to make sure that my search matches are highlighted super clearly, no matter the color scheme, so I give them black text on a yellow background.

" When highlighting search terms, make sure text is contrasting color
:highlight Search ctermbg=yellow ctermfg=black
" Do the same for gvim
:highlight Search guibg=yellow guifg=black
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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