I was happily refactoring my messy vimrc, trying to emulate how the legendary junegunn does his. However, I realised that Vim was becoming less and less responsive the more I type. It appeared that the issue only plagued me when I was editing my vimrc, but not any other files. Needless to say, I was extremely annoyed. I was determined to identify the cause of the problem, and solve it.

The Problem

During my investigation, I read up quite a bit on debugging Vim. Some of the noteworthy things I discovered include this helpful guide for troubleshooting Vim. I also found this great plugin called Bisectly that automates the plugin bisection process. By the way, if you are unfamiliar with the concept of bisection, you can read up more about it here. After much effort, I found that the issue could be attributed to these following three factors.

Factor #1: File Length

It turns out that my vimrc file was getting longer and longer. Writing more comments and functions made my code much more readable and understandable, but it also increased the file length. By right, going from 500 lines to 700 lines should not drastically impact the performance of Vim. Yet, it did. This served as an insight to the second factor in this problem. When a file gets longer, the syntax highlighting takes significantly more effort to work properly.

Factor #2: Regex Engine

Vim’s syntax highlighting relies on the regex engine. Typing :syntax will show you all the syntax items that are active for the current buffer. All that pattern matching requires a fast and reliable regex engine for the syntax highlighting to feel seamless. Every time you scroll the page, it takes a toll on the regex engine. If it is not performant enough, the screen will start to lag as Vim furiously tries to redraw the screen with new highlights. Vim 7.4 introduced a shiny, new regex engine, but many people beg to differ.

Factor #3: Relative Number

The last thing that caught me by surprise was relativenumber. It seems like people have reported performance issues with relative number when syntax highlighting is enabled. You can read more about the issue on GitHub.

The Solution

Funnily enough, relative number was actually the biggest issue. Disabling it with set norelativenumber in my vimrc immediately solved the problem. Unfortunately, I really cannot live without a life-changing feature like relative number. The Vim team did release a patch to address the bug, but applying means rebuilding Vim from its source with the patch applied manually. To me, it was just too much work.

On the other hand, many people suggest reverting to the old regex engine with set re=1, especially for Ruby development. So, I decided to give it a shot. It was a godsend. Vim felt so much faster and responsive with that one extra line in my vimrc. Since I dislike short forms, I went with this:

set regexengine=1

You can read the entry about the option with :help regexengine. Of course, we could also disable syntax highlighting completely with :syntax off. That would literally solve the root of the problem, if you could live with a life without colours.