Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was searching for an answer to this, but haven't found anything yet, so if I missed something obvious, I apologize. I thought it would be a quick google search away, so maybe it's more complicated than I thought.

I'd like to be able to conditionally load plugins based on the type of file I'm editing. For example, when editing python, I don't want the keybindings that vim-sexp installs, but when working on clojure, I do want that plugin to be loaded.

I'm currently using neovim and pathogen, but if there's a different plugin manager that does this for me, I'd be open to switching. Also, if this does exist, does :set syntax whatever switch the plugins? Because that would be super :)

Thanks in advance for any help/direction!

share|improve this question
You are asking a question, but you actually describe another: if a plugin installs global keybindings, disable them (read the plugin docs about how to do it), and re-enable them locally (using autocmds for example) only for the filetypes you want. If the plugin is well written, you don't gain too much by delaying its initialization. But if you don't disable the unwanted global keybindings activated by a plugin, they will still affect you after the plugin is loaded. – VanLaser Mar 21 at 15:39
up vote 8 down vote accepted

You can do this with Vim-Plug. See the README:

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

I think you'll have to use :set filetype whatever instead of set syntax whatever to enable the plugin.

(Some other plugin manager might allow you to do that, see What is the difference between the vim package managers?)

share|improve this answer

Instead of calling execute pathogen#infect() to load all plugins, call pathogen#interpose for every plugin, i.e.:

execute pathogen#interpose('bundle/unicode.vim')
execute pathogen#interpose('bundle/AnsiEsc.vim')

The pathogen API isn't documented outside of the source, but it's simple enough (even though the function names are obscure and non-descriptive). You can also use pathogen#surround()for absolute paths.

You can load plugins conditionally with a basic autocmd:

" Always use this plugin
execute pathogen#interpose('bundle/unicode.vim')

" Only for Python
autocmd FileType python execute pathogen#interpose('bundle/vim-sexp')

If you find you have many of these cases, you could even split it up into different paths:

  • ~/.vim/bundle/always for plugins you always want
  • ~/.vim/bundle/<filetype> for plugins for a specific filetype
  • And then load it like so:

    " Always use this plugin
    execute pathogen#infect('bundle/always/{}')
    " Load filetype plugins if they exist
    autocmd FileType * if isdirectory('/home/martin/.vim/bundle/' . &ft) | execute pathogen#infect('bundle/' . &ft. '/{}') | endif

This way you don't have to add a whole bunch of autocmds for every filetype/plugin.

if this does exist, does :set syntax whatever switch the plugins? Because that would be super :)

  • Once a plugin is loaded, it's loaded. A "plugin" is simply a collection of function, command, and map definitons. I don't know of any straightforward way to "unload' this.

  • It does load plugins for a filetype iff you use :set filetype; In general, you always want to use :set filetype and not :set syntax, as syntax will only set the syntax highlighting, and not the indentation and other settings (e.g. iskeyword, formatexpr, etc.).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.