63

When you search for a word in a file with something like /console.log, all of the instances of console.log are highlighted.

When you're no longer interested in these, the highlighting can be distracting. My current strategy for removing the highlighting is to do something like /asntehua. Is there a proper way to remove this word highlighting?

10 Answers 10

48

As an alternative to :noh, I like to do :let @/="" mapped to a keyboard shortcut.

The difference is that :noh leaves the search term in the search register, so n and N in normal mode resume the search by jumping to the next/previous match and re-highlighting. Using :let @/="", on the other hand, causes the message E25: No previous regular expression and leaves your cursor where it was, which is especially convenient if you don't yet know about ctrl-o yet and accidentally hit n.

This can also be used the other way. To cause vim to highlight some text without jumping to it, you can :let @/="some text"

In these expressions, @ lets you refer to a register, and @/ is the register holding the last search pattern.

31

Simply type

:noh<cr>

(Where <cr> symbolizes a carriage return, i.e. Enter.) The full non-abbreviated version of this command is :nohlsearch.

For convenience, you can have a mapping such as

nnoremap <Leader><space> :noh<cr>

in your .vimrc. Since my leader is Space, this allows me to clear highlighting simply by tapping space twice. (By default, <Leader> is set to \)

Another popular option is to bind it to Ctrl+L, since this is more or less the default for 'redrawn terminal screen', which is very roughly what you're doing:

nnoremap <silent> <C-L> :nohlsearch<CR><C-L>

This has the side-effect of also redrawing the terminal screen, which might be slow if your terminal connection is slow (which is very rare these days).
NOTE: If you're using tpope/vim-sensible then it already offers the <C-l> mapping listed above.

23

Disable search highlighting permanently

Matches won't be highlighted whenever you do a search using /

:set nohlsearch

Clear highlight until next search

:noh

or :nohlsearch (clears until n or N is pressed or a fresh search is performed)


Clear highlight on pressing ESC

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

Clear highlight on pressing another key or custom map

  • Clear highlights on pressing \ (backslash)

    nnoremap \ :noh<return>
    
  • Clear highlights on hitting ESC twice

    nnoremap <esc><esc> :noh<return>
    
  • I added nnoremap <esc> :noh<return><esc> to my vimrc but it's not working. I have jk mapped to escape, could that be why? – mbigras Mar 2 '17 at 2:38
  • What is exactly happening? Maybe you disabled the ESC key? – Sheharyar Mar 2 '17 at 2:40
  • pressing jk does not toggle off the highlighted text (but still works as Esc). Pressing my Esc in the top left of my keyboard does toggle off the highlighted text. – mbigras Mar 2 '17 at 2:43
  • So you have your answer right there. Put your jk map below the ESC map and make sure you are using a simple map instead of noremap – Sheharyar Mar 2 '17 at 2:46
  • 1
    Remapping escape can cause problems with arrow keys, and I'd assume other scenarios that use an escape code. vi.stackexchange.com/questions/11144/… – Rob Mosher Aug 20 at 16:15
2

After trying out several suggested solutions for shortcuts for this in ~/vimrc, I settled on simply adding nmap <esc><esc> :noh<return> to it. The double escape prevents interference with the function of the arrow keys. If you're in insert mode, a triple escape is necessary. This is quick to execute and easy to remember.

2

I added a autocmd to my .vimrc that removes the highlighting anytime you enter insert mode.

 autocmd InsertEnter * :let @/=""
  • 2
    Using nohl instead of let @/="" would be a better approach, since then you don't clobber your current search. – jamessan Sep 19 '18 at 23:57
  • @jamessan it works with the :let @/="" command but autocmd InsertEnter * :noh doesn't for some reason, any idea why? – tobek May 17 at 22:54
  • 1
    Actually I found the reason: according to the docs 'The search-highlighting cannot be switched off with ":nohlsearch" in an autocommand' - details of why: vimdoc.sourceforge.net/htmldoc/autocmd.html#autocmd-searchpat – tobek May 17 at 23:01
1

If you'd like to not highlight after any search, you can use this (better):

:set nohls

or this (worse):

:hi Search NONE
1

When I typed :help incsearch, I found the following in the manual:

augroup vimrc-incsearch-highlight
  autocmd!
  autocmd CmdlineEnter /,\? :set hlsearch
  autocmd CmdlineLeave /,\? :set nohlsearch
augroup END

which I resigned to using since other solutions I tried were not perfect and this solution comes "straight from the horse's mouth", so to speak. It highlights all search matches in the current window.

1
/asdfasdf

This has the advantage that you do not have to type it exaclty. The disadvantage is, that there is a about 1:68719476736 chance in a 4096 byte file that this does not work and your cursor will jump to a different place. This chance increases for larger files.

  • 1
    Welcome on the vi SE! I think the string asdfasdf is special in the sense that it happens far often in practical files, than a random-selected 8-byte sequence. Thus, the chance is far higher. :-) – peterh says reinstate Monica Aug 19 at 22:32
0

I do not think remove hls at all would be a good thing, how about making easy to enable or disable creating a easy shortcut (boolean) In my case -->

" to reselect use gv in normal mode
nnoremap <S-F11> <ESC>:set hls!<cr>
inoremap <S-F11> <C-o>:set hls!<cr>
vnoremap <S-F11> <ESC>:set hls!<cr> <bar> gv

The magic happens because of "!" at the end of the setting

0

I find that it is very rare for me to want highlighting at all and when I need highlighting it is just for a brief moment. My solution is

nnoremap <Leader>/ set hls!<CR>

This enables highlighting when you press \/ and disables it again if you press again. This gives easy access to highlighting in the rare cases when you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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