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:

vim
" Configured for lazy loading on Python files
Plug 'davidhalter/jedi-vim', { 'for': 'python' }
" But when opening a .py file, jedi-vim is not loaded

Symptoms

  • Plugin commands not available after opening a file of the expected type
  • Plugin autoload directory exists but plugin is not sourced
  • :scriptnames does not show the plugin in the loaded scripts list
  • Plugin works when loaded eagerly (without for condition)
  • 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 on missing 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. 1.Enable filetype detection and plugins before plugin manager:
  2. 2.```vim
  3. 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. 1.Manually trigger lazy loading in a FileType autocmd:
  2. 2.```vim
  3. 3.augroup LazyLoadPlugins
  4. 4.autocmd!
  5. 5.autocmd FileType python plug#load('jedi-vim')
  6. 6.autocmd FileType rust plug#load('rust.vim')
  7. 7.autocmd FileType javascript,typescript plug#load('coc.nvim')
  8. 8.augroup END
  9. 9.`
  10. 10.Verify filetype detection is working:
  11. 11.```vim
  12. 12.:set filetype?
  13. 13." Should show: filetype=python (or the expected type)
  14. 14.`
  15. 15.If filetype is not detected, add custom filetype detection:
  16. 16.```vim
  17. 17.augroup FiletypeDetection
  18. 18.autocmd!
  19. 19.autocmd BufRead,BufNewFile *.py set filetype=python
  20. 20.autocmd BufRead,BufNewFile *.rs set filetype=rust
  21. 21.augroup END
  22. 22.`
  23. 23.Check if the plugin loaded:
  24. 24.```vim
  25. 25.:echo exists(':JediDebugInfo')
  26. 26." Should return 2 if the command exists
  27. 27.:scriptnames
  28. 28." Look for the plugin path in the output
  29. 29.`
  30. 30.For packer.nvim (Neovim), use the correct lazy loading syntax:
  31. 31.```lua
  32. 32.use {
  33. 33.'davidhalter/jedi-vim',
  34. 34.ft = 'python',
  35. 35.}
  36. 36.`

Prevention

  • Always place filetype plugin indent on at the top of your vimrc
  • Test lazy loaded plugins by opening files of the target type and verifying functionality
  • Use :PlugStatus to 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.log to identify slow-loading plugins