Incremental search in vim
Tags: vim, search, productivity
Published: 2020-02-24 18:06:25

---
title: Incremental search in vim
published: true
description: How to add incremental searching to your vim editor
tags: vim, search, productivity
cover_image: https://i1.wp.com/pmihaylov.com/wp-content/uploads/2020/02/incsearch-image.png?w=1352
canonical_url: https://pmihaylov.com/incremental-search-vim/
---

<!-- wp:paragraph -->
<p>There is a slight, but obnoxious difference in how the default search works in vim against the way it works in other IDEs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In vim, whenever you click the <strong><em>/ (forwardslash)</em></strong> 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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The way search works in IDEs is that it starts showing results while you're typing the keyword. This way, you can:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Get to the word you need before you even write the whole keyword</li><li>Detect a mistake in your term before you're finished</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>This feature is called incremental search and can be very handy.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> Today, I want to show you how to get it in your vim editor.</p>
<!-- /wp:paragraph -->

<p style="font-size: 14px;"><em> This article is part of the sequence&nbsp;<a href="http://pmihaylov.com/category/boost-your-vim/"> Boost Your VIM</a> where I share my favorite vim plugins and tools which can greatly optimize your productivity and make you a better keystroke ninja.</em></p>

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>The incsearch.vim plugin</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://github.com/haya14busa/incsearch.vim">This plugin</a> will help you get incremental searching out of the box with little to no additional configuration after install.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After you install it and add my configuration, here's how it will look like:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":974,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pmihaylov.com/wp-content/uploads/2020/02/incsearch-demo.gif" alt="incremental search in vim" class="wp-image-974"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Looks good? Then let's get started.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To install the plugin, simply execute this command if you use <a href="https://pmihaylov.com/pathogen-plugin-manager/">my package manager (Pathogen)</a>:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
{% gist https://gist.github.com/preslavmihaylov/1478d3f335aeb4b8ef2d7ff416ee6fcb %}
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>With a bare-bones installation, you need not put anything extra in your <code>.vimrc</code> file. It works out of the box. Here's how it initially looks:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":975,"width":580,"height":350,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://pmihaylov.com/wp-content/uploads/2020/02/incsearch-barebones.gif" alt="barebones incremental search" class="wp-image-975" width="580" height="350"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Customisations</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to be able to quickly jump between results without leaving the search prompt, add these mappings in your <code>.vimrc</code>:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
{% gist https://gist.github.com/preslavmihaylov/2bfc03b125d75c629857f2cb90fb0233 %}
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Now, whenever you want to jump between results, use <code>Tab</code> to go forward and <code>Shift-Tab</code> to go back:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":976,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pmihaylov.com/wp-content/uploads/2020/02/incsearch-custom1.gif" alt="results traversal" class="wp-image-976"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>That final mapping is so that you don't stop the search process when you use the arrow keys.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The final option I like to add is disabling the highlight of search results when the search is over and I've clicked enter.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
{% gist https://gist.github.com/preslavmihaylov/195a62ac067157a07c1501f14aff689d %}
<!-- /wp:html -->

<!-- wp:image {"id":977,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pmihaylov.com/wp-content/uploads/2020/02/incsearch-custom2.gif" alt="no highlighting afterwards" class="wp-image-977"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
{% gist https://gist.github.com/preslavmihaylov/0f2245de22cb728fcbfbf941ebbd8bed %}
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Vim's default incremental search</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As <a href="http://disq.us/p/27irg45">Keith Thompson mentioned</a> in the discussion below this post, vim has incremental search by default, without the need for an extra plugin.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://medium.com/@haya14busa/incsearch-vim-is-dead-long-live-incsearch-2b7070d55250">Some time ago</a>, the original author of the plugin, mentioned in this article, created a patch to add the incremental search feature to vim.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
{% gist https://gist.github.com/preslavmihaylov/6e7955de6ffc73e216c6b43fcf70f4a7 %}
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Just make sure to delete the plugin (simply delete it from the <code>~/.vim/bundle</code> directory) I mentioned and its related configuration if you prefer to go this route.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->