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

When I do a search in vim, I like to have my results highlighted and super-visible, so I give them a bright yellow background and a black foreground in my .vimrc.

" When highlighting search terms, make sure text is contrasting colors
:highlight Search guibg=yellow guifg=black

(This is for GUI versions of vim, like MacVim or Gvim; for command-line, you'd use ctermbg and ctermfg.)

But I sometimes use search with a command, as in c/foo - "change from the cursor to the next occurrence of foo."

In that case, I don't want all the occurrences of foo to be highlighted.

Can I turn off highlighting only in cases where search is used as a movement for a command?

share|improve this question
    
If there's no way to make this a default, is there a way to have an alternate search that doesn't highlight? Like, instead of c/foo, c//foo, where // is a non-highlighting search? – Nathan Long Aug 15 '12 at 15:17
up vote 3 down vote accepted
+150

These naive mappings seem to help.

  • normal search

    Turn highlighting on before doing the search:

    nnoremap / :set hls<CR>/
    
  • motion search

    Turn highlighting off before doing the search:

    nnoremap v/ :set nohls<CR>v/
    nnoremap d/ :set nohls<CR>d/
    nnoremap y/ :set nohls<CR>y/
    nnoremap c/ :set nohls<CR>c/
    

Note that you'd need to setup similar mapping for ?.

share|improve this answer
    
Haven't had a chance to fully explore this suggestion yet, but it may work and at least points me in the right direction. I'd say that deserves the bounty. :) – Nathan Long Aug 22 '12 at 8:24

To permanently disable highlighting of searches:

:set nohlsearch
(or)
:set nohls

But probably you want to keep highlight on. On the other hand, if want to temporary get rid of the highlight, type:

:nohlsearch
(or)
:noh

It will stop highlighting the current search, until you search again.

Even though it's not the perfect answer for you, this is a nice workaround, and this is what I use.

share|improve this answer
    
Actually, I've got an even shorter shortcut for "clear my current search highlighting": I just hit enter if I'm in normal mode. This line is in my .vimrc: :nnoremap <CR> :nohlsearch<CR>/<BS>. It clears the highlighting, starts a new search, and then backspaces to abort the search (thereby clearing the search bar so things look normal again.) This is pretty easy, but I'm so lazy that if I could avoid even having to hit enter, I would like that. :) – Nathan Long Feb 14 '11 at 16:22
    
I got an even easier shortcut: Alt+/ mapped to nnoremap <M-/> :nohlsearch<CR> (note: Alt+/ does not work on some terminals, but works well inside gvim) – Denilson Sá Feb 14 '11 at 21:09
    
I don't quite get it. What do you type and what happens? – Nathan Long Feb 15 '11 at 21:55
    
I've added the remapping thing to my .vimrc. Then, whenever I want to temporary disable the highlighting, I press Alt+/, which is the shortcut to which I mapped it. – Denilson Sá Feb 17 '11 at 17:11
    
Also... as I said, it's a workaround, not exactly a solution. – Denilson Sá Feb 17 '11 at 17:23

You must log in to answer this question.

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