vimtricks.wiki Concise Vim tricks, one at a time.

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 your packpath (usually ~/.vim/pack/ or ~/.config/nvim/pack/)
  • :packadd! (with bang) loads the plugin without running its plugin/ scripts — useful when sourcing vimrc at 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

  • :packloadall forces all opt packages to load immediately — useful for debugging or one-off loading
  • :packadd is 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 matchit plugin (extended % matching) ships with Vim itself in pack/dist/opt/matchit — enable it with :packadd matchit

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?