๐Ÿš€ Speed up vim startup time by conditionally loading plugins.

๐Ÿš€ Speed up vim startup time by conditionally loading plugins.

ยท

2 min read

Hello everyone ๐Ÿ‘‹,

We all love vim plugins. It's a great way to improve your vim experience, but it can increase the startup time of vim. For example, here I am using neovim and I can test the startup of vim by,

vim --startuptime /tmp/vim.log

I recently found that you can conditionally load vim plugins based on commands or filetypes

I use vimplug for loading plugins.

I use nerdtree to toggle the file tree. nerdtree takes a significant time to load. But I don't need to load this plugin unless I press the NerdTreeToggle command. So, I can load this plugin when I need to load this plugin by,

Plug 'tpope/vim-commentary', { 'on': 'Commentary' }
Plug 'preservim/nerdtree', { 'on':  'NERDTreeToggle' }

I can do the same for filetypes,

" load these plugins only on markdown files
Plug 'vim-pandoc/vim-pandoc', { 'for': ['markdown.pandoc', 'markdown'] }
Plug 'SirVer/ultisnips', { 'for': ['markdown.pandoc', 'markdown'] }

๐Ÿ’ก you can detect the filetype by typing :set filetype? on vim

Here are the before and after results,

Before

295.518  008.671: BufEnter autocommands
295.538  000.020: editing files in windows
309.053  013.515: VimEnter autocommands
309.064  000.011: UIEnter autocommands
309.072  000.008: before starting main loop
313.486  004.414: first screen update
313.492  000.007: --- NVIM STARTED ---

After

250.377  007.681: BufEnter autocommands
250.388  000.012: editing files in windows
262.878  012.490: VimEnter autocommands
262.887  000.009: UIEnter autocommands
263.694  000.472  000.472: sourcing /usr/local/share/nvim/runtime/autoload/provider/clipboard.vim
263.710  000.351: before starting main loop
267.812  004.101: first screen update
267.817  000.006: --- NVIM STARTED ---