Is it possible to not trigger my normal search highlighting when using search as a movement?

7

4

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?

Nathan Long

Posted 2011-02-10T14:29:58.513

Reputation: 20 371

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 – 2012-08-15T15:17:56.117

Answers

3

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 ?.

romainl

Posted 2011-02-10T14:29:58.513

Reputation: 19 227

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 – 2012-08-22T08:24:35.863

1

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.

Denilson Sá Maia

Posted 2011-02-10T14:29:58.513

Reputation: 9 603

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 – 2011-02-14T16:22:33.640

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á Maia – 2011-02-14T21:09:47.120

I don't quite get it. What do you type and what happens? – Nathan Long – 2011-02-15T21:55:42.313

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á Maia – 2011-02-17T17:11:25.833

Also... as I said, it's a workaround, not exactly a solution. – Denilson Sá Maia – 2011-02-17T17:23:37.160

hlsearch is "off" by default, no need to urn it "off". – romainl – 2012-08-21T15:51:18.057