Introduction
Vim plugin managers like vim-plug support lazy loading plugins to improve startup time. When lazy loading is configured to trigger on filetype but the plugin does not load when opening a file, the plugin's functionality is completely absent:
" Configured for lazy loading on Python files
Plug 'davidhalter/jedi-vim', { 'for': 'python' }
" But when opening a .py file, jedi-vim is not loadedSymptoms
- Plugin commands not available after opening a file of the expected type
- Plugin autoload directory exists but plugin is not sourced
:scriptnamesdoes not show the plugin in the loaded scripts list- Plugin works when loaded eagerly (without
forcondition) - Filetype is detected correctly but plugin still does not load
Common Causes
- Filetype detection runs before plugin manager processes lazy load triggers
- Plugin manager's
plug#load()function not called on FileType event filetype plugin onmissing from vimrc- Plugin's
ftplugin/directory does not match the detected filetype - Plugin manager initialized in the wrong order relative to filetype detection
Step-by-Step Fix
- 1.Enable filetype detection and plugins before plugin manager:
- 2.```vim
- 3.filetype plugin indent on
call plug#begin('~/.vim/plugged') Plug 'davidhalter/jedi-vim', { 'for': 'python' } Plug 'rust-lang/rust.vim', { 'for': 'rust' } call plug#end() ```
- 1.Manually trigger lazy loading in a FileType autocmd:
- 2.```vim
- 3.augroup LazyLoadPlugins
- 4.autocmd!
- 5.autocmd FileType python plug#load('jedi-vim')
- 6.autocmd FileType rust plug#load('rust.vim')
- 7.autocmd FileType javascript,typescript plug#load('coc.nvim')
- 8.augroup END
- 9.
` - 10.Verify filetype detection is working:
- 11.```vim
- 12.:set filetype?
- 13." Should show: filetype=python (or the expected type)
- 14.
` - 15.If filetype is not detected, add custom filetype detection:
- 16.```vim
- 17.augroup FiletypeDetection
- 18.autocmd!
- 19.autocmd BufRead,BufNewFile *.py set filetype=python
- 20.autocmd BufRead,BufNewFile *.rs set filetype=rust
- 21.augroup END
- 22.
` - 23.Check if the plugin loaded:
- 24.```vim
- 25.:echo exists(':JediDebugInfo')
- 26." Should return 2 if the command exists
- 27.:scriptnames
- 28." Look for the plugin path in the output
- 29.
` - 30.For packer.nvim (Neovim), use the correct lazy loading syntax:
- 31.```lua
- 32.use {
- 33.'davidhalter/jedi-vim',
- 34.ft = 'python',
- 35.}
- 36.
`
Prevention
- Always place
filetype plugin indent onat the top of your vimrc - Test lazy loaded plugins by opening files of the target type and verifying functionality
- Use
:PlugStatusto see which plugins are loaded and which are deferred - Add filetype autocmd debug logging:
autocmd FileType * echo "Filetype: " . &filetype - Keep lazy loading configuration documented with comments explaining why each plugin is lazy loaded
- Benchmark Vim startup time with and without lazy loading to verify the benefit
- Use
vim --startuptime startup.logto identify slow-loading plugins