Manually and conditionally load plugins for Vim - vim

I have two different projects that I'm working on (let's call them projA and projB) that have their own Vim plugins.
Each plugin folder has an ftdetect, ftplugin, plugin and syntax subfolder, and each deals with the same type of files (.cpp, .html, etc).
If I load both sets of plugins then nothing I want works right so I need a way to only load the plugin that corresponds to the project I'm working on.
My idea is to detect what my current working directory is via getcwd() and then only load the relevant plugin, but I have no idea how to manually load a single plugin.
I'm currently using Vundle to manage the rest of my plugins.

With vim-plug
The vim-plug plugin manager supports loading plugins conditionally.
This is straight from their readme:
" On-demand loading
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

Package managers like Vundle and Pathogen separate each plugin into its own subtree and concatenate all those paths into the 'runtimepath' option so that Vim considers all of them. That makes it particularly simple to disable plugins: Just prevent the inclusion of the plugin's subtree into 'runtimepath'.
Vundle references plugins in ~/.vimrc via Bundle 'foo/bar' commands, so you just have to put a conditional around it:
if getcwd() ==# '/work/cpp'
Bundle example/cpp
else
Bundle example/other
endif
conventional approach
With a conventional, single ~/.vim/ configuration hierarchy, you'd have to resort to suppressing plugin loads by setting the canonical g:loaded_PluginName inclusion guard. This requires support from the plugin, and mostly won't work for ftplugins, indent, and syntax scripts.

Related

How do I selectively load vim plugins?

I am using vim-plug and I want to load a certain plugin only for some specific files. How can I do that?
Suppose I have plugin A. How can I load plugin A only for python and cpp?
Similarly, can I prevent vim from loading some plugin B for html and php files?
autocmd FileType cpp,py source pluginAPath
From the README file of vim-plug:
" On-demand loading
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
This did not help me much, so I looked elsewhere.
Apparently there is a filetype plugin option used to define behaviour specific to filetypes. So, to add settings for .py files, add filetype plugin on in .vimrc and create ~/.vim/ftplugin/py.vim to add settings there.

Vim plugins declared in ftplugin do not work

Gvim is behaving weird and I can't find the reason. I use Vundle, and all the plugins declared in my .vimrc are working fine. I declared some additional settings and plugins in .vim/after/ftplugin/java.vim.
The mappings work fine, but the plugins do not work. If I choose a different file in my current gvim session, I get those error messages:
Error detected while processing function vundle#config#bundle[2]..<SNR>14_check_bundle_name:
line 2:
Vundle error: Name collision for Plugin Raimondi/delimitMate. Plugin Raimondi/delimitMate previously used the name "delimitMate". Skipping Plugin Raimondi/delimitMate.
Vundle error: Name collision for Plugin artur-shaik/vim-javacomplete2...
[comment: same error message for all plugins declared in the ftplugin]
I noticed, that if I run :VundleInstall the plugins are suddenly working (the error messages stay when I change the file, no plugins are installed when I use the command).
Here is the beginning of my .vimrc:
syntax on
set guifont=Inconsolata\ Medium\ 12
set nocompatible
set t_Co=256
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
"[comment: all plugins I use for every filetype]
call vundle#end() " required
filetype plugin indent on
and this is my java.vim file:
filetype off
"to automatically close brackets
Plugin 'Raimondi/delimitMate'
"omni-complete for Java
Plugin 'artur-shaik/vim-javacomplete2'
"use tab to navigate through insert completion
Plugin 'ervandew/supertab'
filetype plugin indent on
"needed to make javacomplete2 working properly
autocmd FileType java setlocal omnifunc=javacomplete#Complete
My OS is Ubuntu 16.04.
You're mistaken regarding what ftplugins are doing and what they should contain.
Ftplugins are loaded once per buffer, every time a new buffer is created/opened.
They are meant to contain buffer local definitions:
:map <buffer> keybinding action
:iab <buffer> keybinding expanded sequence
:setlocal option[=value]
:command -b CommandName :Action
:let b:option = value
set the localleader (but be certain it's done before any other ftplugin for the same filetype) (EDIT: localleader is actually a global setting, my mistake)
They could then load other things that work the same way with :runtime or :so. They could contain functions, but it's best to define them into autoload plugins since Vim7. They may contain buffer local menu definitions, but this requires a plugin as this is not standard.
They are definitively not meant to contain global definition like the ones you have defined. It's not really the place to load global plugins that'll stay activated afterwards.
I know that some plugins manager load plugins on the fly depending on the type of the file we work on. I've never shared this need when we are using properly defined ftplugins, and lightweight plugins that only define a few mappings and keep their functions into autoload plugins.
Last thing, ftplugins are supposed to contain anti reinclusion guards. On a typical scenario this is not that useful. Many use b:did_ftplugin for that purpose, but I avoid this variable as I prefer to have as many ftplugins (for a same filetype) as themes (one that specializes the brackets pairs, one that defines the mapping to automatically expand a switch statement from the type of a variable, one that defines abbreviations for control statements, and so on). As a consequence I cannot use the same guard for all files.
All your :Plugin commands are supposed to be between these two lines:
call vundle#begin()
" :Plugin commands go here
call vundle#end()
Try another plugin manager if you absolutely need lazy loading.

How to make vim pathogen to reload plugins?

Is it possible to make vim to reload pathogen plugins without restarting vim?
I have opened vim with many files, then I add plugin to:
~/.vim/bundle
Since now I'd like to force vim to use the new plugin.
Pathogen just manipulates the 'runtimepath' option; the situation with plugin reloads is therefore the same as with the plain default plugin structure. (Other plugin managers may offer this kind of reload / dynamic enable functionality; I suppose you want to stick with Pathogen.)
To retroactively enable a plugin in a running Vim session, you need to :source all (usually that's only one) plugin scripts that have been added. For a plugin named foobar, that would be:
:source ~/.vim/bundle/foobar/plugin/foobar.vim
If you can get Pathogen to re-initialize the 'runtimepath' (or augment it yourself via :set rtp+=~/.vim/bundle/foobar), you can also use the shorter
:runtime plugin/foobar.vim
If you use a modern version of vim, you can use its built-in package manager, which has a convenient function to reload all plugins:
:packloadall
http://vimhelp.appspot.com/repeat.txt.html#%3Apackloadall
I was in the same boat before util I find an awesome plugin(vim-reload) to do these stuffs automatic in an amazing way.You should have a shot at this plugin.

How can I reload a VIM plugin after VIM was started?

I am modifying an installed VIM plugin and in another Terminal tab I am testing the results.
Each time I want to test the changes I have to restart VIM.
Is there any faster way to do this process? How can I reload a VIM plugin after VIM was started?
The plugin is installed via Vundle
I have tried to run :so % that is supposed to reload the .vimrc file but I still cannot see my changes.
If there weren't an inclusion guard (if ! exists('g:loaded_pluginname') ...), you could simply :runtime! plugin/pluginname.vim (or :source % if it's currently opened) and all plugin definitions would be re-read.
But as most plugins (correctly) use such a guard, you need to :unlet that variable first:
:unlet g:loaded_pluginname | runtime! plugin/pluginname.vim
My ReloadScript plugin can do this with one command if the guard name adheres to the canonical naming, and the scriptease plugin has such a command, too.
Edit: Some plugins use differently named guard variables, or other ways that prevent reloading. The plugins from mattn (like emmet.vim) are quite elaborate; maybe there's some special mechanism; I don't use that plugin. You could ask the author for advice, though.

what are the differences between Vundle and NeoBundle?

I would like to have a nice and easy way of managing vim plugins.
I found NeoBundle and Vundle.
What are the main differences between them? I know that NeoBundle is a fork of Vundle, but what is it that makes it different?
What do you use?
Here is an article written (in Japanese) by the author of NeoBundle Shougo, about why he wrote NeoBundle and how NeoBundle differs from Vundle.
Actually, NeoBundle is a fork of Vundle. Shougo added some features to a fork of Vundle but lately found he couldn't follow the upstream Vundle's development, so he made the fork to a new plugin now called NeoBundle.
The differences summarized as below:
Rename the commands from Vundle, replace Bundle to NeoBundle. (Example: BundelInstall to NeoBundleInstall).
Add support for vital.vim, a vim utility library written by thinca.
Neobundle works even you have set the shellslash option other than the default.
Add support for vimproc, a launcher plugin written by Shougo.
Add an interface for unite.vim written by Shougo, he also notes it as the major motive for writing NeoBundle.
Add support for plugins that hosted as a Subversion/Mercurial repository, but it is still an experimental feature now.
(UPDATED) Now NeoBundle adds a lazy loading feature where Vundle doesn't have. It allows you to load plugins at some user-defined time point, not only during the vim initialization where .vimrc is loaded.
I use vundle because it suffices my needs (I used pathogen before). But you can take a try at NeoBundle.
(UPDATE) NeoBundle has stopped active development now and will be replaced by dein.vim, which is Shougo's another brand new plugin manager. As a side note, you can also take a look at vim-plug which I'm currently using.

Resources