vim: highlight match under cursor

Long story short, I wrote a plugin that does this. The code is here.

The hlsearch option is a convenient feature of vim that highlights all occurences of the previous search pattern. However, if the there are many occurrences of the pattern currently visible on the screen, it can be difficult to tell where the cursor is located when cycling through them with n or N.

I thought it would be convenient for the occurrence of the search pattern currently beneath the cursor to be highlighted differently to make it fast and easy to identify. I didn't find an option or plugin that allowed me to do this, so I decided to write my own. My search for an existing solution was by no means exhaustive, so if you know of one, feel free to let me know.

You can get an idea of the plugin's behaviour by looking at the screenshots below. In the first, the default behaviour without the plugin is shown. The pattern match is highlighted, but it is a little hard to see where the cursor is, even with cursorline enabled. In the second screenshot, the plugin has highlighted the match under the cursor in red, making its location very obvious.

Example of search highlighting without the plugin.
Example of search highlighting without the plugin.
Example of search highlighting with the plugin.
And now with the plugin.

The match is highlighted with a customizable format, which I like to set the same as the IncSearch match group. The plugin works with all the search commands (/, ?, *, #, n, N) and the smartcase and ignorecase options.

The plugin is available on GitHub, where you can also see a GIF demonstrating the behaviour.