Vim clear last search highlighting

VimFull Text-SearchHighlight

Vim Problem Overview


After doing a search in Vim, I get all the occurrences highlighted. How can I disable that? I now do another search for something gibberish that can't be found.

Is there a way to just temporarily disable the highlight and then re-enable it when needed again?

Vim Solutions


Solution 1 - Vim

To turn off highlighting until the next search:

:noh

Or turn off highlighting completely:

set nohlsearch

Or, to toggle it:

set hlsearch!

nnoremap <F3> :set hlsearch!<CR>

Solution 2 - Vim

From the VIM Documentation

To clear the last used search pattern:

:let @/ = ""

This will not set the pattern to an empty string, because that would match everywhere. The pattern is really cleared, like when starting Vim.

Solution 3 - Vim

You can do

:noh

or :nohlsearch to temporarily disable search highlighting until the next search.

Solution 4 - Vim

I found this answer years ago on vim.org:

Add the following to your .vimrc:

"This unsets the "last search pattern" register by hitting return
nnoremap <CR> :noh<CR><CR>

Thus, after your search, just hit return again in command mode, and the highlighting disappears.

Solution 5 - Vim

From http://twitter.com/jonbho/status/2194406821

" Clear highlighting on escape in normal mode
nnoremap <esc> :noh<return><esc>
nnoremap <esc>^[ <esc>^[

The second line is needed for mapping to the escape key since Vim internally uses escape to represent special keys.

Solution 6 - Vim

My original solution (below) was dirty and wasteful. I now use :noh

Like many features in Vim, the right way is often not easily discoverable.

--

(DO NOT DO THIS)

Search for an unlikely character sequence (mash the keys on the home row):

/;alskdjf;

This works in vim and less, and it's easier to remember/type than @ShaunBouckaert's logically cleaner solution "to clear the last used search pattern":

:let @/ = ""

A potential downside is that it adds junk to your search history.

Solution 7 - Vim

The answers proposing :noh or :nohlsearch (e.g., Matt McMinn’s) are correct for temporarily disabling search highlighting – as asked in the original question.

I thought I'd contribute a mapping that I find useful in Normal mode:

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

By default, CtrlL in Vim clears and redraws the screen. A number of command line programs (mostly those using the GNU Readline library, such as Bash) use the same key combination to clear the screen. This feature is useful in the situation where a process running in the background prints to the terminal, over-writing parts of the foreground process.

This Normal mode mapping also clears the highlighting of most recent search term before redrawing the screen. I find the two features complement each other and it’s convenient to use one CtrlL for both actions together rather than create a separate mapping for disabling search highlighting.

NB: noremap is used rather than map as otherwise, the mapping would be recursive.

Tip: I usually remap Caps Lock to Ctrl to make it easier to type such key combinations; the details for doing this depend on your choice of OS / windowing system (and are off-topic for this answer). Both the following tips include information on mapping Caps Lock to Ctrl as well as Esc:

Solution 8 - Vim

Remapped to in my .vimrc.local file, quick and dirty but very functional:

" Clear last search highlighting
map <Space> :noh<cr>

Solution 9 - Vim

nnoremap <cr> :noh<CR><CR>:<backspace>

That way I get rid of :noh shown in the commandline, when hitting enter after the search.

: is like starting entering a new command, Backspace clears it and puts the focus back into the editor window.

Solution 10 - Vim

Disable search highlighting permanently

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

:set nohlsearch

:noh

or :nohlsearch (clears until n or N is pressed)


Clear highlight on pressing ESC

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

Clear highlight on pressing another key or custom map

  • Clear highlights on pressing </kbd> (backslash)

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

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

Solution 11 - Vim

I generally map :noh to the backslash key. To reenable the highlighting, just hit n, and it will highlight again.

Solution 12 - Vim

This is what I use (extracted from a lot of different questions/answers):

nnoremap <silent> <Esc><Esc> :let @/=""<CR>

With "double" Esc you remove the highlighting, but as soon as you search again, the highlighting reappears.


Another alternative:

nnoremap <silent> <Esc><Esc> :noh<CR> :call clearmatches()<CR>

According to vim documentation:

> clearmatches() > > Clears all matches previously defined by |matchadd()| and the > > |:match| commands.

Solution 13 - Vim

To turn off highlighting until the next search

:noh

Visual Illustration

Solution 14 - Vim

Turn off highlighting as soon as you move the cursor:

Install vim-cool. It was created to fix this specific problem. It turns off highlighting as soon as you move the cursor. Be warned though, it requires a recent vim version!

Solution 15 - Vim

There are two 'must have' plugins for this:

  1. sensible - Ctrl-l for nohlsearch and redraw screen.
  2. unimpared - [oh, ]oh and coh to control hlsearch.

Solution 16 - Vim

If you have incsearch.vim plugin installed, then there is a setting to automatically clear highlight after searching:

let g:incsearch#auto_nohlsearch = 1

enter image description here

Solution 17 - Vim

[Janus for VIM and GVIM][1] has a number of baked-in things for newbs like me, including

<leader>hs - toggles highlight search

which is exactly what you need. Just type \hs in normal mode. (The leader key is mapped to \ by default.)

HTH.

[1]: https://github.com/carlhuda/janus "Jenus for VIM/GVIM"

Solution 18 - Vim

My guess is that the original question concerned not disabling search highlighting but simply clearing the highlighting from the last search. The solution of searching for a gibberish string, which the original poster mentioned, is one I've been using for some time to clear highlighting from a previous search, but it's ugly and cumbersome.

Several suggestions I've found to add nnoremap ... to ~/.vimrc have the effect here of putting vim into replace mode at startup, which isn't at all what I want. The simplest solution I've found is to add the line

nmap <esc><esc> :noh<return>

to my ~/.vimrc. This hews to the KISS principle and doesn't interfere with the arrow keys, which using a single <esc> does. A double-<esc> is required in command mode (or a triple-<esc> from insert or replace mode) to clear highlighting from a previous search, but from a UI perspective this makes the operation about as simple as possible.

Solution 19 - Vim

I personnaly like to map esc to the command :noh as follow:

map <esc> :noh<cr>

I wrote a whole article recently about Vim search: how to search on vanilla Vim and the best plugin to enhance the search features.

Solution 20 - Vim

This will clear the search highlight after updatetime milliseconds of inactivity.

updatetime defaults to 4000ms or 4s but I set mine to 10s. It is important to note that updatetime does more than just this so read the docs before you change it.

function! SearchHlClear()
	let @/ = ''
endfunction
augroup searchhighlight
	autocmd!
	autocmd CursorHold,CursorHoldI * call SearchHlClear()
augroup END

Solution 21 - Vim

If you want to be able to enable/disable highlighting quickly, you can map a key to

" Press F4 to toggle highlighting on/off, and show current value.
:noremap <F4> :set hlsearch! hlsearch?<CR>

Just put the above snippet in you .vimrc file.

That's the most convenient way for me to show and hide the search highlight with a sing key stroke

For more information check the documentation http://vim.wikia.com/wiki/Highlight_all_search_pattern_matches

Solution 22 - Vim

I just use the simple nohl below and no plugins are needed.

:nohl

Solution 23 - Vim

One more solution by combining 2 top answers:

"To clear the last used search pattern:
nnoremap <F3> :let @/ = ""<CR>

Solution 24 - Vim

you can use :noremap to turn on/off for the search result,like this

:noremap <F3> :set hls! hls?<CR>

Solution 25 - Vim

I use the following in my ~/.vimrc

nnoremap <Leader><space> :noh<Enter>

This makes it very easy and quick to clear the current highlighted search. My leader key is mapped to \ so this makes the action very easy to perform with my right pinky finger and thumb.

Solution 26 - Vim

I think mixing @ShaunBouckaert and Mar 19 '09 at 16:22 answers is a good compromise :

" Reset highlighted search
nnoremap <CR> :let @/=""<CR><CR>

Press Enter and the highlighted text is no longer highlighted, while search highlighting is still enabled.

Solution 27 - Vim

Based on @baruch-even answer, you can delete search term on ESC double press while in normal mode with:

nnoremap <esc> :let @/ = ""<return><esc>
nnoremap <esc>^[ <esc>^[

Solution 28 - Vim

I don't like it either. I found it tiresome to enter :nohl all the time ... so i put the following mapping in my .vimrc

noremap <C-_> :nohl<cr>:<backspace>

The first bit (:nohl<cr>) clears the highlighting; the second bit (:<backspace>) is a trick to clean the command line. The search is still there in the background, so if you simply hit n it'll re-highlight and take you to the next occurrence.

Solution 29 - Vim

Define mappings for both behaviors, because both are useful!
  • Completely clear the search buffer (e.g., pressing n for next match will not resume search)
  • Retain search buffer, and toggle highlighting the search results on/off/on/... (e.g., pressing n will resume search, but highlighting will be based on current state of toggle)
" use double-Esc to completely clear the search buffer
nnoremap <silent> <Esc><Esc> :let @/ = ""<CR>
" use space to retain the search buffer and toggle highlighting off/on
nnoremap <silent> <Space> :set hlsearch!<CR>

Solution 30 - Vim

I added this to my vimrc file.

command! H let @/=""

After you do a search and want to clear the highlights from your search just do :H

Solution 31 - Vim

Any solution by nohlsearch is not prefered, what about next search? The best answer is created by let @/ = "", but I do not like map by function key on mac, so here is mine:

let mapleader=','
let g:mapleader=','
nnoremap <leader>cs :let @/ = ""<cr>

Solution 32 - Vim

You can alternatively add mapping on your .vimrc file as,

> nmap <C-N> :nohlsearch

and then you can press

> Ctrl+N

to anytime to clear the highlighted text

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriel SolomonView Question on Stackoverflow
Solution 1 - VimMykola GolubyevView Answer on Stackoverflow
Solution 2 - VimShaun BouckaertView Answer on Stackoverflow
Solution 3 - VimMatt McMinnView Answer on Stackoverflow
Solution 4 - VimView Answer on Stackoverflow
Solution 5 - VimBaruch EvenView Answer on Stackoverflow
Solution 6 - VimmillerdevView Answer on Stackoverflow
Solution 7 - VimAnthony GeogheganView Answer on Stackoverflow
Solution 8 - VimavocadeView Answer on Stackoverflow
Solution 9 - VimsjasView Answer on Stackoverflow
Solution 10 - VimSheharyarView Answer on Stackoverflow
Solution 11 - VimcavemanView Answer on Stackoverflow
Solution 12 - VimPablo Olmos de Aguilera C.View Answer on Stackoverflow
Solution 13 - VimarcseldonView Answer on Stackoverflow
Solution 14 - Vim00prometheusView Answer on Stackoverflow
Solution 15 - VimdkiyatkinView Answer on Stackoverflow
Solution 16 - VimDeqingView Answer on Stackoverflow
Solution 17 - VimPaul-SebastianView Answer on Stackoverflow
Solution 18 - VimLindsay HaisleyView Answer on Stackoverflow
Solution 19 - VimMatthieuView Answer on Stackoverflow
Solution 20 - VimKevin CoxView Answer on Stackoverflow
Solution 21 - VimAmaynutView Answer on Stackoverflow
Solution 22 - VimStrykerView Answer on Stackoverflow
Solution 23 - VimskywinderView Answer on Stackoverflow
Solution 24 - VimcaopengView Answer on Stackoverflow
Solution 25 - VimJohn SlavickView Answer on Stackoverflow
Solution 26 - VimEkansView Answer on Stackoverflow
Solution 27 - VimdinigoView Answer on Stackoverflow
Solution 28 - VimricardoView Answer on Stackoverflow
Solution 29 - VimardnewView Answer on Stackoverflow
Solution 30 - Vimpixel 67View Answer on Stackoverflow
Solution 31 - VimCui MingdaView Answer on Stackoverflow
Solution 32 - VimLeOView Answer on Stackoverflow