1. Overview

Highlighting search results in Vim is helpful. But after we have found what we are looking for, sometimes we want to remove the highlighting while we continue working.

In this short tutorial, we’ll go through a few ways to control highlighting in a Vim search.

2. Setup

To start, let’s set up a simple text example. Let’s open Vim:

$ vim

Then, after pressing i, let’s insert some text:

Here is a test line. 
And this line is another test line.

We can now hit ‘Esc’ to exit insert mode. With this text, let’s do a quick search by typing:

/line

The cursor should stop at the next occurrence of “line”. Pressing n should cycle through the three instances of “line” we have in our example. It’s worth noting that if we don’t see highlighting at this point, it may already be turned off. In that case, we can simply turn it on:

:set hls

Now that we have a setup, let’s dig into a few commands to control search result highlighting.

3. Disable Search Highlight

Once we perform a search and find what we’re looking for, there are a couple of easy ways we can quickly turn off highlighting. First, we can use:

:nohlsearch

This turns off the highlighting until we do another search. We can also use the shortened version for the same effect:

:noh

Another way is to do a second search where we look for some nonsense text:

/bljdfj

This gets rid of the search highlighting from our previous search and probably won’t match or highlight any other text. It’s not the most accurate method, but it’s quick and effective.

4. Disable Search Highlight for a Session

Next let’s disable search highlighting for an entire Vim session:

:set nohlsearch

Now search highlighting will be turned off for the entire session. We can see this by running our /line search again and seeing the cursor go to the next occurrence but without highlighting. Exiting Vim clears this setting and later sessions will again have search results highlighted.

We can also re-enable search highlighting for the current session:

:set hlsearch

The highlighting will be back on now.

5. Disable Search Highlight for All Sessions

Let’s now look at how to turn off search highlighting for all Vim sessions. We do this by adding a command to our .vimrc file:

set nohlsearch

This runs at Vim startup and turns off search highlights.

6. Conclusion

In this short article, we looked at a few different ways to turn off search highlighting in Vim. We looked at how to do a quick search of something random to clear highlighting, as well as how to configure both the current session and Vim’s defaults.

Comments are closed on this article!