How do I load a plugin on demand using Vim's native package system?
Answer
:packadd {name}
Explanation
Vim 8+ and Neovim have a built-in package manager. Plugins placed in pack/*/opt/ directories are not loaded automatically at startup — you control when they load by calling :packadd {name}. This gives you lazy-loading without any external plugin manager.
How it works
- Start plugins (
pack/*/start/) — loaded automatically at startup - Optional plugins (
pack/*/opt/) — loaded only when you call:packadd {name}where{name}is the plugin's directory name - Vim searches all
pack/*/opt/directories in yourpackpath(usually~/.vim/pack/or~/.config/nvim/pack/) :packadd!(with bang) loads the plugin without running itsplugin/scripts — useful when sourcingvimrcat startup
Example
Directory layout:
~/.vim/pack/tools/opt/matchit/
Load it on demand:
:packadd matchit
Or load it conditionally in your vimrc only for certain filetypes:
augroup go_plugins
autocmd!
autocmd FileType go packadd vim-go
augroup END
Tips
:packloadallforces alloptpackages to load immediately — useful for debugging or one-off loading:packaddis idempotent — calling it twice on an already-loaded package is safe- Prefer
pack/*/opt/for large or rarely-used plugins to keep startup time low - The
matchitplugin (extended%matching) ships with Vim itself inpack/dist/opt/matchit— enable it with:packadd matchit