2324

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?

techenthu
  • 158
  • 11
Gabriel Solomon
  • 29,065
  • 15
  • 57
  • 79
  • 7
    :set invhlsearch will disable the highlighting if its already highlighted and enable it if it isn't. You can map it to say Shift-H. – puffadder Jan 03 '11 at 09:47
  • 1
    @StewartJohnson doing that in my .vimrc files seems to interfere/break with :set mouse=a. Any ideas why? This error only happens in iTerm2 – Charlie Parker Jun 16 '14 at 13:54
  • I have typed so much gibberish over the years to clear that highlight (really inefficient for multi-GB text files). The gibberish search is essentially a second search which wastes compute time. Great question! – Megatron Nov 26 '14 at 21:16
  • 24
    @StewartJohnson Be careful! Your top-rated comment with mapping cause errors and unexpected behaviour as described in the comments below to http://stackoverflow.com/a/1037182/1698467 – skywinder Feb 19 '15 at 18:42
  • @StewartJohnson, somehow this 'nnoremap' breaking mouse scrolling (iTerm, vim8). working ok if entering ':noh' manually tho – vigilancer Nov 24 '16 at 12:30
  • 12
    The `nnoremap :noh` solution suggested by @StewartJohnson works nicely in GUI vim, but causes problems with arrow keys and other ESC-encoded keys when running vim in a terminal. *Don't put it in your* `~/.vimrc` without wrapping it in `if has('gui_running')` ... `end`. – jbyler Sep 28 '17 at 18:16

32 Answers32

2044

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>
qwertzguy
  • 15,699
  • 9
  • 63
  • 66
Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
  • 210
    This is not clear from this answer and comments : note that `set nohlsearch` will disable the highlighting for next searches as well. The behaviour of `:noh` is very different : the highlighting will be automatically reenabled when you perform a new search or even when you do something related to the current search (e.g. when you type `n` to go to the next item). – Skippy le Grand Gourou Dec 20 '12 at 14:42
  • 5
    I map it to because that's my go-to keystroke for cleaning up the screen. Here is my mapping: `nmap :nohlsearch:match:diffupdate` – joeytwiddle Oct 08 '18 at 08:54
  • @Rainning, is this what you would put in .vimrc? If not, what can you put in .vimrc to permanently enable :noh after every search? – Liam Clink Apr 13 '21 at 03:02
  • 9
    For reader in 2021: `nnoremap :noh`. – NeoZoom.lua Apr 13 '21 at 03:14
  • @LiamClink: I've updated the command, should be `nnoremap`. And yes you should put it in your .vimrc. I don't understand your question, since the next time you hit `/` on your key board and enter a new keyword to search, to old ones are all replaced, so you only need to cleanup by `` yourself after every search. – NeoZoom.lua Apr 13 '21 at 03:18
  • `set nohlsearch` works in ~/.vimrc to permanently disable search highlighting, but vim is still highlighting text as I enter my search which I find very distracting. Is there a way to turn that off as well? I've observed this in vim 8.2 – brookbot Jul 10 '21 at 23:57
  • @Rainning exactly what I need! Really like the combo ``. Thank you! – kohane15 Oct 11 '21 at 08:10
  • I just use /zz... Done that so often it is part of my muscle memory :-) – Rob Wells Sep 26 '22 at 17:03
  • this is terrible. setting it manually each time is no better than clearing it by hand. also doesn't distinguish between subs and searches, turns off for everything. shaun's answer is far better. – Ed_ Aug 29 '23 at 16:52
808

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.

gcochard
  • 11,408
  • 1
  • 26
  • 41
Shaun Bouckaert
  • 8,417
  • 1
  • 17
  • 20
  • It will disable it forever but not temporary. – Mykola Golubyev Mar 18 '09 at 09:41
  • 140
    ...which would be a better solution to his problem as I understand it. – Shaun Bouckaert Mar 18 '09 at 09:46
  • 2
    Your answer does indeed answer his question better, by turning the highlight on and off. However, if the issue was only that the last search stayed highlighted after you found what you were looking for, which is how I initially understood the question, then my method allows you too clear the search. – Shaun Bouckaert Mar 18 '09 at 13:05
  • @Mykola: I agree that you answered the question perfectly correctly, but your solution requires extra key-presses / steps. e.g. Search, turn off highlighting, turn on highlighting, search again. Shaun's solution will only require me to Search, clear highlighting, search again. – Andy Mar 19 '09 at 10:24
  • @Mykola: Clarification to the above - it's working from the mindset that after I've searched and modified some text, I want to get rid of the search entirely. With my personal workflow I won't want to turn off the search results until I've finished with them. – Andy Mar 19 '09 at 10:31
  • As I understood "then reenable it when nedeed again" is important part of the question. If not that I'd use let @/="" – Mykola Golubyev Mar 23 '09 at 12:46
  • 1
    @Mykola I actually thought that he only asked how to disable and re-enable it again because he wasn't sure you could clear the search, so he was looking for the next best option. – Shaun Bouckaert Mar 25 '09 at 00:15
  • 9
    This is what I was looking for! I voted it up, the question is slightly vague. This led me to think this is what he wanted: "I now do another search for something gibberish that can't be found". Because that is what I was doing to clear the search, but not disable it so the next search would highlight again. – claytron Aug 31 '10 at 18:42
  • 55
    This is what I thought the question was about... here is a command I made to quicken clearing of the search string (and therefore removing all highlights): `:command C let @/=""` Using this allows you to type `:C` to clear the search string... very quick and doesn't affect future searching or highlighting. – Jason Mar 09 '11 at 13:31
  • This is what I was hunting for as well. I like highlight search. However, once I am done with it, I want to clear the pattern, not turn it completely off! Thanks. – Rick Apr 30 '12 at 18:08
  • This is the better answer that the poster didn't know he wanted. As far as I can tell, this is what he's asking for, and that's what I was looking for, so, upvoted. – Stavros Korokithakis Jun 02 '12 at 17:25
  • 1
    So, this is better than `:noh` if you do not have `hlsearch` set? Since this will wipe the search target, I cannot press `n` to go to the next match. It seems like this reduces functionality in favor of not-using `hlsearch` – New Alexandria Jul 22 '13 at 16:04
  • If you use the solution provided in the answer. To "re-enable" the highlight you can press / then up arrow then enter. This will perform a new search with the search string you used previously from the current cursor position. – BeowulfNode42 Feb 05 '14 at 23:11
  • 1
    Can someone wise in the ways of VIM explain to a vim newb how this works? :) – jcreamer898 May 20 '14 at 20:09
  • 14
    @jcreamer898, see vim help on "registers" (`:he registers`). Vim has several different built-in registers that hold text, sometimes for yanked data, sometimes for last search, etc. When you do a search, vim puts the pattern in the "/" register, which you reference using `@/`. You can also assign values to registers using `@regname=value` where `regname` is the is the name of the register. So, `@/=""` is simply setting register "/" to an empty string (except that for the "/" register vim will clear the last search if it contains an empty string). – Ben Davis Jun 06 '14 at 22:32
  • 9
    Add this to your .vimrc to get Ctrl+/ to clear the last search: `noremap :let @/ = ""` – angrydust Sep 15 '14 at 21:46
  • @theguywholikeslinux nice. I use nnoremap so that it works in normal mode. – Peng Zhang Nov 08 '14 at 22:41
  • This works better than `:noh` in my opinion, because even though `:noh` will clear the highlights, after a re-sourcing of vim all the highlights will show up again. With this solution, a re-source will not show any highlights. –  Feb 22 '17 at 09:44
  • 3
    This has the side effect that by clearing the pattern, you lose the ability to re-search by pressing `n`. I prefer `:noh` (bound to a key) because I can clear the highlights but then easily get them back by pressing `n`. – jbyler Sep 28 '17 at 18:14
  • 1
    When I use this, the highlighting comes back every time I reopen the file with vim. What gives? I thought this solution was supposed to clear the pattern permanently, not just temporarily disable highlighting. `:noh` on the other hand doesn't re-enable the highlighting when I reopen the file. – WD40 Mar 06 '18 at 18:57
  • 3
    I've been binding it like `nnoremap / :let @/=""/` for the past 4 years and works better for me. Just press `/` again and it clears up. – bentinata May 31 '21 at 03:30
646

You can do

:noh

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

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Matt McMinn
  • 15,983
  • 17
  • 62
  • 77
  • 3
    thanks, this was helpful ... any command for returning the highlight ? – Gabriel Solomon Mar 19 '09 at 13:47
  • 20
    :nohs just shuts off the current highlighting. If you have :set hlsearch then it will continue to highlight your searches. – greyfade Mar 19 '09 at 15:55
  • 6
    I think this is what the OP was searching for (although Shaun's answer works too). I'm upvoting this for being shorter. I know he accepted the nohls answer, but setting it will disable all search highlighting, even if another search is tried afterwards. – Thiago Arrais Apr 16 '12 at 14:18
  • 5
    This is probably what the OP wanted in the first place and is the shortest, best answer IMHO. –  May 31 '12 at 15:05
  • Perfection is attainable even in this mortal realm! I will use this more than :help, and don't want to pollute : nnoremap :noh – Sam Watkins Oct 03 '12 at 08:24
  • setting it manually each time is no better than clearing it by hand. shaun's answer is far better. – Ed_ Aug 29 '23 at 16:50
358

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.

  • 41
    I think this is a *much* better solution than the one that's actually been accepted -- either this one or the other one where it's mapped to instead. – Stewart Johnson Sep 01 '10 at 22:49
  • 23
    This is a much better answer to "nnoremap :noh" because it doesn't cause strange behavior. – Dennis Mar 13 '12 at 19:36
  • 20
    An important thing to note, make sure you don't put any comments on the right hand side of a remap. They will be interpreted as commands for the remap as opposed to comments. – codybuell Nov 15 '12 at 20:37
  • 15
    The comment is wrong; the command does **not** unset the last search pattern but simply switches off the highlighting of the current results; you can hit `n` to find the next occurrence (which will be highlighted again). BTW, when scripting, I would avoid the abbreviated form, and instead write `:nohlsearch`. – Tobias Feb 25 '14 at 13:42
  • 39
    Add to avoid the display flashing and leaving noh in the command line - :nnoremap :nohlsearch – Peter N Lewis Jun 18 '14 at 05:58
  • 4
    Or to bind this to Ctrl+L, which clears and redraws the screen: `nnoremap :noh` – Fernando Correia Jul 02 '14 at 14:34
  • 4
    I love this one but why the two ``s? If you want to keep your cursor where it is, use only 1 `` – Abdo Nov 08 '14 at 07:19
  • 2
    @Abdo And then you can *never* use Enter to move down. – Jonathon Reinhart Dec 21 '14 at 08:35
  • 2
    @JonathonReinhart I see, thanks! The thing is, I don't use Enter and it bothers me that every time I want to turn highlight off, my cursor's position changes :-) – Abdo Jan 06 '15 at 22:02
  • I think this should be the most relevant answer looking at the question and pre-assuming OP's intention ! – Sumit Singh Jun 13 '18 at 14:08
151

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.

Community
  • 1
  • 1
Baruch Even
  • 2,581
  • 1
  • 17
  • 23
  • 13
    Just to be clear, this lets you clear the search highlighting by pressing the Escape key – Gavin Miller Oct 12 '10 at 16:58
  • 7
    For some reason, this makes my terminal vim start up as if "c" was pressed. Any ideas? – Jacob Jan 24 '11 at 16:02
  • 32
    This caused some very strange behavior when I tried it: on startup, regular vim movements (eg j, k) would cause lines to be removed. nnoremap :noh didn't cause these problems. I couldn't track down the root cause. – oasisbob Aug 21 '11 at 05:48
  • 9
    hmm, I had the same problem as those above. Don't use this as-is without testing unless you like deleting your text randomly... – Derek Litz Nov 26 '11 at 19:41
  • 4
    [This question](http://stackoverflow.com/questions/11940801/mapping-esc-in-vimrc-causes-bizzare-arrow-behaviour) elaborates on some of the problems with this approach being pointed out by commenters. – David Tchepak Aug 21 '12 at 02:11
  • I would add this also: `inoremap :noh` – qed May 04 '13 at 14:12
  • 3
    I add this functionality to my `ctrl-l` key which normally clears / redraws the screen. Remapping `esc` is always tricky business - I'd avoid it because nearly all terminals rely on the escape key code for keys which have no inherent code (like arrow keys etc.). – jw013 May 31 '13 at 15:02
  • Weird behaviour after mapping it to esc. Works fine when mapped to Space though. – psx Jun 21 '13 at 13:07
  • Having tried this myself, mapping to ESC is a bad idea - it causes some unexpected problems. This is because of the way vim processes keys - for example, when you press the up arrow key, vim receives the code: [A -- thus after remapping the key, pressing the up arrow will now work properly as "[A" loses its special meaning. – Tom Lord Jul 22 '13 at 14:15
  • 1
    So to summarise, as others have already, just map this sequence to , or , or \, or whatever else you feel is most natural to you. But leave alone. – Tom Lord Jul 22 '13 at 14:16
  • 9
    using "nnoremap :noh" makes vim always starting in "REPLACEMENT" mode. – user1850133 Mar 30 '14 at 20:51
  • Thanks this solution (with the second line fix) is great and intuitive and seems to work with the arrows :) I'll give it a go. But if you know of some solution or mapping that uses the / search character, like d/ or c/, it might also be a good option,,, – Joel.O Sep 24 '16 at 10:38
  • 3
    The `nnoremap :noh` solution works nicely in GUI vim, but causes severe problems with arrow keys and other ESC-encoded keys when running vim in a terminal. *Don't put it in your* `~/.vimrc` without wrapping it in `if has('gui_running')` ... `end`. – jbyler Sep 28 '17 at 18:20
46

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.

millerdev
  • 10,011
  • 2
  • 31
  • 27
  • 25
    Not only is this a dirty solution (which I myself used until now, no offense), another downside is that it adds an ugly red banner saying "pattern not found" at the bottom of the window, which is almost as annoying as the highlighting itself and requires further action to be discarded (e.g. "search + backspace"). – Skippy le Grand Gourou Dec 20 '12 at 14:49
  • 6
    It can also take a few seconds, as it _actually searches_ the entire rest of the file. That's really annoying if the file is large. Of course, you could mark your current position, jump to the beginning, and search backwards for a character, but that's similarly dirty. – Blacklight Shining Jan 15 '14 at 04:54
  • Yeah, I also use this ugly solution but I always use '/qwe' which for some reason neeever matches anything in any file. – Juan Campa Feb 07 '14 at 00:49
  • 6
    This is the solution most will have used **before** learning about the `:nohl` command. Once I learned about the latter, I immediately stopped to do those searches. – Tobias Feb 25 '14 at 13:34
  • 10
    This is not an answer; it's just recommending to do what OP already does: "I now do another search for something gibberish that can't be found." – Reinstate Monica -- notmaynard Sep 23 '14 at 19:04
  • I use /qw to clear search highlight, in English all words starting with q is followed by u and never w ;) – ticktock Mar 09 '16 at 14:40
  • 4
    If you want a foolproof and short pattern just use `/$4`. It's not just unlikely to match anything but actually completely IMPOSSIBLE. `$` must always be followed by a newline or be the end of the file, and 4 is neither a new line nor the end of the file. (The reason for 4 specifically is just that it is on the same key, so it's really easy to type quickly) – semicolon Jun 27 '16 at 16:02
42
nnoremap silent <cr> :noh<cr><cr>

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.

sjas
  • 18,644
  • 14
  • 87
  • 92
38

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:

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
35

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

" Clear last search highlighting
map <Space> :noh<cr>
avocade
  • 1,313
  • 1
  • 14
  • 19
30

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)


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>
    
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • 6
    The `nnoremap :noh` solution works nicely in GUI vim, but *causes severe problems* with arrow keys and other ESC-encoded keys when running vim in a terminal. *Don't put it in your* `~/.vimrc` without wrapping it in `if has('gui_running')` ... `end`. Also consider adding the `` which avoids flashing the `:noh` command in the status bar. – jbyler Sep 28 '17 at 18:25
27

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
caveman
  • 1,755
  • 1
  • 14
  • 19
22

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.
  • I faced some bugs in top solutions, so to save your time trying all the answers. **Use this solution!** Edit your `.vimrc` and put the following entry in it:`nnoremap :let @/=""` – ani627 Aug 09 '16 at 05:33
  • But that's what it says O.o. Maybe you could upvote? – Pablo Olmos de Aguilera C. Aug 11 '16 at 02:59
  • I'm supporting your solution... It is the best one IMO... And I have also upvoted it.. :) – ani627 Aug 11 '16 at 04:58
  • 2
    Clearing the search register with `@/=""` has the side effect of making `n` not work to get your search back. I prefer `:noh` which keeps the previous search in the register for re-use. – jbyler Sep 28 '17 at 18:26
22

To turn off highlighting until the next search

:noh

Visual Illustration

arcseldon
  • 35,523
  • 17
  • 121
  • 125
18

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!

00prometheus
  • 767
  • 7
  • 20
  • This easily solves the problem. Remapping is just too much for me – Fatima Jul 11 '20 at 13:58
  • Finally, no more extra keys to press lol – apostl3pol Mar 24 '22 at 23:29
  • Should be a default option! – abu_bua Apr 26 '22 at 23:25
  • after looking through alot of the answers here, I skipped this one initially cuz I didn't want to use a plugin for such a simple thing. Then, after looking at all the strange stuff you have to do (and especially: potential unwanted sideeffects), or live with having a DEDICATED KEY for clearing search highlighting - like, who in their right mind in 2022 would accept using precious key-mapping-space for just clearing the search highlight?? / - OR override something like or with potential sideeffects this makes no sense. So, I bit the bullet and installed this. Never looked back. – emilBeBri Dec 10 '22 at 13:16
11

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

Deqing
  • 14,098
  • 15
  • 84
  • 131
10

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.
dkiyatkin
  • 614
  • 7
  • 11
  • unimpaired changed `co` to `yo` in v2, so it's `yoh`. See https://github.com/tpope/vim-unimpaired/blob/v2.0/doc/unimpaired.txt and https://github.com/tpope/vim-unimpaired/issues/150 – myrdd Mar 24 '19 at 13:21
  • `coh` still works, although there is a message suggesting to use `yoh` if you use `coh` – avinashkrsharma May 28 '20 at 16:32
9

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.

Lindsay Haisley
  • 392
  • 3
  • 9
  • What do you mean by the single interfering with arrow keys? – hansmosh Sep 12 '17 at 20:59
  • 1
    @hansmosh See https://unix.stackexchange.com/questions/28713/why-does-mapping-esc-cause-arrow-keys-to-fail-in-vim I use two lines in my .vimrc as follows `nmap :noh - to turn off search highlighting and nmap \` :set hlsearch - to turn on search highlighting` – Lindsay Haisley Sep 14 '17 at 15:53
8

Janus for VIM and GVIM 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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Paul-Sebastian Manole
  • 2,538
  • 1
  • 32
  • 33
8

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

:nohl

Stryker
  • 5,732
  • 1
  • 57
  • 70
8

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.

Matthieu
  • 948
  • 8
  • 8
  • Remapping can be problematic because a timeout is added automatically, making it slow to reenter normal mode. https://vi.stackexchange.com/questions/16148/slow-vim-escape-from-insert-mode – apostl3pol Mar 24 '22 at 23:08
7

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kevin Cox
  • 3,080
  • 1
  • 32
  • 32
6

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

Amaynut
  • 4,091
  • 6
  • 39
  • 44
5

One more solution by combining 2 top answers:

"To clear the last used search pattern:
nnoremap <F3> :let @/ = ""<CR>
skywinder
  • 21,291
  • 15
  • 93
  • 123
  • Clearing the search register with `@/=""` has the side effect of making `n` not work to get your search back. I prefer `:noh` which keeps the previous search in the register for re-use. You can bind `` to that with `nnoremap :noh` – jbyler Sep 28 '17 at 18:29
4

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

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

caopeng
  • 914
  • 13
  • 23
4

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.

John Slavick
  • 10,179
  • 5
  • 28
  • 25
3

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ekans
  • 997
  • 1
  • 8
  • 16
3

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>^[
dinigo
  • 6,872
  • 4
  • 37
  • 48
  • I think the second binding is OK, but the first is dangerous. Remapping `` works nicely in GUI vim, but *causes severe problems* with arrow keys and other ESC-encoded keys when running vim in a terminal. *Don't put it in your* `~/.vimrc` without wrapping it in `if has('gui_running')` ... `end`. Also consider adding the `` which avoids flashing the `:noh` command in the status bar. – jbyler Sep 28 '17 at 18:31
3

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.

ricardo
  • 8,195
  • 7
  • 47
  • 69
3

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>
ardnew
  • 2,028
  • 20
  • 29
  • With this, if I press followed by an arrow key, it messes the buffer content because the Esc at the beginning of the arrow key is combined with the first I've pressed, and vim is then left with the rest of the arrow key sequence which is garbage. And no, I'm not ready to give up on arrow keys for `hjkl` :) – Eric Jul 07 '21 at 07:40
  • @Eric I cannot reproduce it with gVim 8.2.2824 . Could you elaborate a bit? Also, try preceed `` with `` – john c. j. Sep 08 '21 at 01:15
  • Unfortunately remapping `` like this will usually cause an annoying delay when exiting insert mode. See https://vi.stackexchange.com/questions/16148/slow-vim-escape-from-insert-mode. – apostl3pol Mar 24 '22 at 22:59
2

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>
Cui Mingda
  • 803
  • 1
  • 6
  • 15
1

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

pixel 67
  • 1,530
  • 18
  • 22
1

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

Mou Sam Dahal
  • 273
  • 2
  • 10
  • Please [edit] your answer to ensure that it improves upon other answers already present in this question. – hongsy Jan 19 '20 at 13:54