# Fix Vim Plugin Lazy Loading Not Triggering on Filetype
You have configured lazy-loaded plugins in Vim, but they do not activate when you open the relevant file type. For example, your Python LSP plugin does not start when you open a .py file, or your TypeScript plugin does not load for .ts files.
Understanding Lazy Loading
Lazy loading delays plugin initialization until it is needed, reducing Vim's startup time. Common lazy-loading triggers include:
- Filetype: Load when editing a specific file type
- Command: Load when a specific command is invoked
- Key mapping: Load when a specific key is pressed
- Event: Load on a specific Vim event
The Filetype Plugin Issue
With vim-plug, a common filetype-based lazy-load configuration looks like this:
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig', { 'for': ['python', 'typescript', 'javascript'] }
Plug 'mfussenegger/nvim-dap', { 'for': 'python' }
Plug 'lukas-reineke/indent-blankline.nvim', { 'for': ['python', 'go', 'rust'] }
call plug#end()If the plugin does not load when you open a Python file, check these issues.
Issue 1: Filetype Not Detected
Vim may not detect the filetype correctly:
:set filetype?If this returns filetype= (empty), Vim does not know the file type. Fix filetype detection:
filetype plugin indent onEnsure this line is in your .vimrc before any plugin configuration. Without it, Vim does not trigger filetype events.
For files without extensions, manually set the filetype:
:set filetype=pythonOr add a modeline to the file:
# vim: set filetype=python:Issue 2: Plugin Dependencies Not Loaded
If a lazy-loaded plugin depends on another plugin that is also lazy-loaded, the dependency may not be available:
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig', { 'for': 'python' }
Plug 'hrsh7th/cmp-nvim-lsp' " Depends on nvim-lspconfig
call plug#end()If cmp-nvim-lsp tries to configure itself before nvim-lspconfig loads, it fails silently. Fix by using explicit on or for for both:
Plug 'hrsh7th/cmp-nvim-lsp', { 'for': 'python' }Issue 3: Autocmd Timing
Filetype-based plugins often use autocmd FileType to set up configurations. If the autocmd is defined after the FileType event fires, it never triggers.
In Lua (Neovim):
```lua -- WRONG: autocmd defined after the file is already opened vim.cmd("autocmd FileType python require('lspconfig').pyright.setup{}")
-- CORRECT: use an augroup in init.lua vim.api.nvim_create_autocmd("FileType", { pattern = "python", callback = function() require("lspconfig").pyright.setup{} end, }) ```
Issue 4: Using packer.nvim Lazy Loading
For Neovim with packer:
use {
'neovim/nvim-lspconfig',
ft = {'python', 'typescript'},
config = function()
require'lspconfig'.pyright.setup{}
require'lspconfig'.tsserver.setup{}
end,
}If the config function is called but the LSP server does not start, check that the config function correctly handles all filetypes:
```lua use { 'neovim/nvim-lspconfig', ft = {'python', 'typescript'}, config = function() local lspconfig = require'lspconfig'
vim.api.nvim_create_autocmd("FileType", { pattern = "python", callback = function() lspconfig.pyright.setup{} end, })
vim.api.nvim_create_autocmd("FileType", { pattern = {"typescript", "javascript"}, callback = function() lspconfig.tsserver.setup{} end, }) end, } ```
The key insight: config runs when the plugin is first loaded (on the first matching filetype), but you need to set up autocmds for each filetype within the config function.
Debugging Lazy Load
Check if the plugin is loaded:
:echo get(g:, 'loaded_plug')Or for packer:
:lua print(vim.inspect(package.loaded))Check which autocmds are defined:
:autocmd FileTypeThis shows all FileType autocmds. If your plugin's autocmd is not listed, the lazy-load configuration is incorrect.
Force load a lazy plugin manually:
:PlugLoad nvim-lspconfig " For vim-plugOr for packer:
:lua require('packer').loader('nvim-lspconfig')