8

I know it's possible to stop Vim from highlighting on any search, but is there a way to make it highlight on regular searches but not, for instance, substitutions?

I will often highlight a block of text then do something like:

:s/^/#/

to comment out the whole block. But then I have an ugly yellow bar running up and down the left side of my screen and I have to :noh every time to clear it.

I want highlighting to remain on regular /searches. Is this possible?

ib.
  • 27,830
  • 11
  • 80
  • 100
Derek
  • 1,196
  • 11
  • 32
  • Not sure if anything like this will work, but maybe it would be possible to change your 'search' highlight group to look like normal text, and then remap the '/' search key somehow to `:match NewSearchGroup /searchterm/. I'd love to also hear a better way though. – Tim Feb 13 '13 at 21:31
  • 1
    This doesn't answer your question BUT you should really use [tComment](https://github.com/tomtom/tcomment_vim) (or a similar commenting plugin) for commenting out text blocks. You'll be able to use motions for commenting and can easily comment anything, including XML. – Daan Bakker Feb 13 '13 at 22:54
  • Maybe you can use [this answer](http://stackoverflow.com/a/13636203/778118) to get pointed in the right direction... – jahroy Feb 14 '13 at 07:16

2 Answers2

5

This doesn't answer your question fully but for me having the same problem it helped: I added this command to easily deactivate the highlighting after a search or a search-and-replace

nnoremap <esc> :noh<return><esc>

(from Vim clear last search highlighting)

Community
  • 1
  • 1
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
3

My solution: put this in your vimrc file

set nohlsearch

noremap * :set hlsearch<CR>:nohlsearch<CR>*
noremap / :set hlsearch<CR>:nohlsearch<CR>/
noremap ? :set hlsearch<CR>:nohlsearch<CR>?

nnoremap <F4> :set invhlsearch<CR>
inoremap <F4> <ESC>:set invhlsearch<CR>gi
nnoremap <CR> :set nohlsearch<CR>

What it does:

  1. Searches always enable highlights.
  2. Use Enter to turn off highlights until the next search, but not the next substitute. Pressing Enter again will not turn them back on though.
  3. Use F4 to turn off highlights until the next search, but not next substitute. Pressing F4 again will toggle the highlights for the last search pattern --- search OR substitute --- if there was one.

Some notes on highlights that are never really explained well:
When the 'hlsearch' option is on, all future searches/substitutes will turn on "highlight visibility". The current "highlight visibility" is not really an option you can directly query or set independently, but you can independently turn OFF highlight visibility with the command :nohlsearch (this is not the same as :set nohlsearch, because the next search will enable visibility again).

In addition, whenever you run the command :set hlsearch there are two effects: It sets the option AND it makes vim forget if you've ever typed :nohlsearch. In other words, changing 'hlsearch' (either on or off) will force the current "highlight visibility" to logically match.

Ein
  • 1,553
  • 3
  • 15
  • 22
  • Thanks @Ein! Your notes about `:set hlsearch` [helped me figure out](https://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting) how to get rid of highlighting during a mapping and reenable it without any side effects like match flickering or error messages. – phatskat Jan 25 '19 at 05:19