There is a slight, but obnoxious difference in how the default search works in vim against the way it works in other IDEs.
In vim, whenever you click the / (forwardslash) you start writing a word you want to find in the current file. The problem is that you have to write the whole word and click enter before you start seeing the results.
This can work fine and you can cope with it most of the times, but it can start getting in the way pretty quickly once you start searching for longer words or phrases, whose exact identifier you can get wrong pretty easily. At that point, you have to start all over again with the search and be very careful about writing the keywords precisely.
The way search works in IDEs is that it starts showing results while you're typing the keyword. This way, you can:
- Get to the word you need before you even write the whole keyword
- Detect a mistake in your term before you're finished
This feature is called incremental search and can be very handy.
Today, I want to show you how to get it in your vim editor.
This article is part of the sequence Boost Your VIM where I share my favorite vim plugins and tools which can greatly optimize your productivity and make you a better keystroke ninja.
The incsearch.vim plugin
This plugin will help you get incremental searching out of the box with little to no additional configuration after install.
After you install it and add my configuration, here's how it will look like:
Looks good? Then let's get started.
To install the plugin, simply execute this command if you use my package manager (Pathogen):
With a bare-bones installation, you need not put anything extra in your
.vimrc file. It works out of the box. Here's how it initially looks:
If you want to be able to quickly jump between results without leaving the search prompt, add these mappings in your
Now, whenever you want to jump between results, use
Tab to go forward and
Shift-Tab to go back:
That final mapping is so that you don't stop the search process when you use the arrow keys.
The final option I like to add is disabling the highlight of search results when the search is over and I've clicked enter.
This is personal preference. I have a key binding for disabling search results highlighting manually by clicking escape twice. I used to use that for this purpose in the past:
I'm still keeping this key binding around in case I get something highlighted accidentally. But at least for search results, I've discovered that I prefer to not keep them highlighted after the search is over.
Vim's default incremental search
As Keith Thompson mentioned in the discussion below this post, vim has incremental search by default, without the need for an extra plugin.
Some time ago, the original author of the plugin, mentioned in this article, created a patch to add the incremental search feature to vim.
However, not all of the plugin features were included in the main vim codebase. This enables you to have incremental search without additional plugins, but still lack e.g. keeping highlighting after you select a match by clicking enter.
This is why I still prefer using the original plugin. However, if you prefer adding this feature to vim without an extra plugin (and the caveat I mentioned doesn't bother you), here's how to do it:
Just make sure to delete the plugin (simply delete it from the
~/.vim/bundle directory) I mentioned and its related configuration if you prefer to go this route.
Searching in vim is one of those features which works Okay, but not perfect. This is a recipe for sticking to the devil we know, when paradise is one step away.
In this article, I've showed you the way to incremental search paradise. Try it now, and you'll feel reborn. You'll wonder how couldn't have you known this already.