1. Overview

Incremental search in text editors displays real-time search results as the user types, making it easier to find content without waiting for the entire search term. Such an add-on search feature improves productivity, especially when working with lengthy files or complex codebases. Consequently, most modern editors offer incremental search as a standard feature.

In this tutorial, we’ll learn how to use the incremental search feature in the Vim editor.

2. Scenario Setup

For our purpose of learning about incremental search, let’s take a look at the data.txt file:

$ cat data.txt
apple apple banana
cherry cherry cherry
date date elderberry
fig fig grape grape
kiwi kiwi lemon
mango mango mango
banana cherry apple
lemon lemon fig
grape grape kiwi
elderberry cherry date

We must note that the file contains duplicate words too.

Now, we can use the vim command to open the data.txt file:

$ vim data.txt

We’re ready to get started.

We’ll be in Vim’s normal mode when we just opened the file. However, we can ensure it by pressing the Esc key.

Further, to enable the incremental search feature, we can set the incsearch option in the command mode:

:set incsearch

We must note the colon(:) at the beginning. It takes us from the normal mode to the command mode, so we can type the entire command as it is.

That’s it! Our feature is enabled now.

Lastly, we can even verify that we were successful in enabling the feature by checking its value using the set command:

:set incsearch?
incsearch

It’s worth noticing that we appended a question mark (?) at the end to query the current value of the incsearch option.

To observe the real-time matches of the search query, first, we can go into normal mode by pressing the Esc key. Once we’re in normal mode, we can start typing our search text:

/<search_text>

So, let’s see this in action by searching for the “g” character:

Incremental Search - Search g

We must note that the editor shows us the first matching result in the file. Further, we didn’t press the enter key to send the search query.

Next, let’s type “r” as the next character in our search text to confirm that Vim is searching as we’re typing the text:

Incremental Search Part2

Fantastic! We can see that Vim highlighted the first matching result as soon as we typed the next character.

5. Incremental Search With Highlighting

By default, Vim highlights only the first occurrence of the matching result. However, if we want to see all occurrences of the current search pattern, we can enable the search highlighting feature. Furthermore, the best part is that search highlighting and incremental search can work together.

First, let’s set the hlsearch option in the command mode to enable the highlighting feature:

:set hlsearch

Next, let’s start searching for “appl” to see that Vim keeps highlighting the matching results with each keystroke:

Incremental Search With Highlight Part2

We can see that all three occurrences of “appl” are highlighted.

6. Disabling Incremental Search and Highlighting

We might want to turn off the incremental search and highlighting feature based on personal preferences.

Let’s start by seeing how we can use the set command to turn off the highlighting:

:set nohlsearch

Similarly, we can turn off the incremental search feature with the set command:

:set noincsearch

That’s it. It’s pretty straightforward.

Lastly, we can verify that we’ve turned them off by querying the hlsearch and incsearch options:

:set hlsearch?
nohlsearch
:set incsearch?
noincsearch

We can notice that Vim displays that both options are disabled.

7. Configuring .vimrc

Once we understand our default preferences for the incremental search, we can configure it in the .vimrc file. For our use case, let’s enable both incremental search and search highlight:

$ cat ~/.vimrc
set incsearch
set hlsearch

Now, each time we open a file, Vim loads this configuration. So, we’re all set to work in our customized editing environment.

8. Conclusion

In this article, we explored the incremental search feature in Vim. Furthermore, we learned how to use incremental search along with the search highlight feature.

Lastly, we customized the .vimrc file to enable these features based on our preference.