19

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!

1
  • 1
    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, 2016 at 15:39

2 Answers 2

22

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 plugin managers?)

1
9

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.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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