I am a power vim user. I have synced between machines .vimrc file with collection of plugins and little tweaks I use. Mostly, I love vim for it’s super speed on any hardware. It works the same great on AMD Athlon processor from 2003 and on Intel Core i5 6’s gen. processor.
But now I am working on project with huge codebase and lengthy directory tree. When I first started a neovim editor for it, it started for about 5 or 6 seconds. It wasn’t great enough for me, because I close-open vim often, for commiting, pushing changes to repo, runing tests, etc.
So I decided to add several optimizations in my .vimrc file. Since I’m using vim-plug plugin manager, I added some lazy loading for plugins, when they are needed in fact instead of loading them on every start (for example, autocompletion engine is not needed until I start insert mode, NERDTree only if I am opening it) and made some extensions to be loaded only while opening several file types (like html5, stylus).
After those optimizations, vim now starts nearly instantly.
Here’s where the problem started
But after a week of active usage, I found out that vim started freezing for some time nearly every time I switch mode. It even made me to commit some errors into repo, because vim was freezing so often, so I couldn’t realize, what did I do.
I started tracing, what’s wrong. I started tracing vim’s io operations using iotop (thanks to advice of Sviatoslav Sydorenko) and found out that every time I enter INSERT mode, huge reading operation has been invoked with
tags file in root of directory.
Than I’ve realized, whats wrong. The problem was in next line:
So every single time I entered INSERT mode, the YouCompleteMe plugin was fully reloaded. It fully restarted it’s completion server! Every single time!
The good way of lazy loading
The right solution is as simple as possible: just check, whether the plugin is already loaded. Like on snippet below.
After that all of my headache with these lags stopped, and it made vim great again. As for me, at last 😊.
Disclaimer: I am not a native English speaker, so I can make mistakes. If you’ve found any of them, feel free to correct me, I’ll be very glad for that.