vim

How to Clear Vim Search Highlighting

Vim is known for its rich functionality and one of its key functions is searching. In Vim searching for a pattern can lead to issues especially when all the search instances are highlighted which can be a distraction.

Let’s explore various approaches to clear Vim’s search highlighting.

Note: For the instructions in this guide, I am using Vim on Linux (Ubuntu). Vim is available for other operating systems, so the instructions will be the same.

How to Clear Search Highlighting in Vim

Although the search highlighting of all the search instances can be a useful functionality; it can be distracting and unwanted. For example, if you have made a search and when you get into the INSERT mode the search highlighting will remain there instead of going away.

Note: The search highlighting is disabled by default in the Vim editor.

You will only see all the search instances highlighted if you have enabled the search highlighting. The below given sections will focus on how to clear the last search highlighting using different methods.

Clear Search Highlighting Temporarily

To enhance focus and reduce distractions, it is crucial to know how to clear the search highlighting in Vim.

To clear the search highlighting till the next search get into the NORMAL mode by pressing the Esc key and then use :nohlsearch or :noh commands:

:nohlsearch

Type the command:

Hit the Enter to execute the command:

Or use the shorthand version of the command:

:noh

These commands disable the search highlight for the current session.

Clear Search Highlighting For the Current Session

If you want to clear search highlighting for the current session then you need to use the set command. Get into the NORMAL mode and type the following command:

:set nohlsearch

Clear Search Highlighting For All Sessions

If you want to permanently clear the search highlighting for every session then you need to put the set nohlsearch in the vimrc file.

Note: You need administrative privileges (sudo) to access and edit the vimrc file.

To access the vimrc file use:

sudo vim /etc/vim/vimrc

Type the following command in the end of the file.

set nohlsearch

Save and quit the file by pressing SHIFT+ZZ or typing :wq in the NORMAL mode.

Now, whenever you open the Vim session the search highlighting will be disabled. However, you can enable the search highlighting at any time by removing the command from the file.

Note: It is advisable to create a user-specific vimrc file using vim ~/.vimrc file instead of modifying the system files.

Clear Search Highlighting by Mapping a Shortcut Key

Not everyone would like to type the command repeatedly to clear search highlighting. Similarly, not everybody would like to permanently disable this feature. So, what’s the solution?

I like to create a shortcut key to clear the search highlighting using Vim’s keymapping feature. Let’s map shift+c keys to clear the search.

:map <S-c> :noh<CR>

In the above command, the map keyword is used to map the key, <S-c> indicates the shift and c keys. While :noh is no search highlight command and <CR> stands for carriage return used for the Enter key.

Note: If you type the keymapping command in the current Vim session then it will expire after the session.

To make it a permanent shortcut key put this command in the vimrc file.

After adding the command save and quit the file by pressing SHIFT+ZZ (or :wq) in the NORMAL mode.

To list the mapped keys type :map command in the Vim session:

:map

The shift+c or C has been mapped for clear search highlights.

Conclusion

Vim is a powerful text editor full of undiscovered features. Search highlighting is another useful feature but can be a problem due to a shortcoming. The search highlights are not clear even if you start editing the file again. To disable it use the :set nohlsearch command. To permanently get rid of it then put the same command in the vimrc file. It is advisable to create a shortcut key to clear search highlighting using the map function.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.