How do I selectively load vim plugins? - vim

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.

Related

How filetype plugin on changes loading

What occurs when the filetype plugin option is changed? For example, from the docs it says:
When loading filetype plugins has been enabled :filetype-plugin-on, options
will be set and mappings defined.
Does this mean that if filetype plugin is off, then vim will not add certain directories to the vim runtime? Or what exactly does this parameter do, I'm a bit confused?
What this command does, it simply loads a relevant script from $VIMRUNTIME.
For filetype plugin on it's $VIMRUNTIME/ftplugin.vim; for filetype plugin off it's $VIMRUNTIME/ftplugof.vim; for filetype off it's $VIMRUNTIME/ftoff.vim, and so on.
Basically ftoff.vim clears filetypedetect auto-group (the one which traps BufRead); and ftplugof.vim clears filetypeplugin group (then one which traps FileType).
I suggest to everyone who is interested in internals to explore the code himself. The source worth a thousand words.

adding new file type to ultisnips

I am trying to add for the cuda (.cu) files. The basic objective is to first make all c,cpp snippets available for the cu files and then add additional support. The first thing I did to test is to set the filetype inside vim
set ft:cpp.c
and this works. Then I tried to go to the vim-snippets/snippets and vim-snippets/UltiSnips and tried to copy the cpp.snippets file to cu.snippets. But this is not working (not working as in --the snippets are not detected--) . I have also added
au BufNewFile,BufRead *.cu set ft=cuda
au BufNewFile,BufRead *.cuh set ft=cuda
in my .vimrc. Even after this it is not working.
I also checked the UltiSnipsSnippetDirectories. It is pointing to Ultisnips.
I also tried creating a cu.snippets which just tries to extend cpp (nothing else). This is also not working.
As a side question: As far I understand https://github.com/honza/vim-snippets has two folders with snippets. snippets/* for the snipmate based ones and UltiSnips/* for the ultisnips based ones. However the inc snippet is only provided on the c.snippets in snippets directory (not in ultisnips). But strangely inc works on c files for me. I am positive that I am not using snipmate. How could this happen? Am I missing something. or is it that ultisnips can understand both formats?
Ultisnips uses Vim's filetype detection system. So to see what filetype Vim thinks you have use the :set filetype? command.
If that's incorrect, you can try
autocmd BufRead,BufNewFile *.cu setfiletype cuda
Also, I used Vundle, and I used call vundle#rc(), but I needed to change that to call vundle#begin() and call vundle#end()
I think, some plugin in your plugins list conflicts with your custom filetype detection config. Just faced with the same issue (tried to declare custom filetype for Jest typescript tests for react). My filetype settings overrides by peitalin/vim-jsx-typescript plugin.
So you should to switch off some of yor installed cpp plugins for detect culprit.

Does Vim automatically load filetype-specific plugins for custom filetypes?

I understand that the recommended method for defining filetype-specific behavior in Vim is with .vim files and filetype plugin option. To add settings for .html files, for instance, I would add filetype plugin on in my .vimrc and add the settings to ~/.vim/ftplugin/html.vim.
All examples of this method that I can find, however, are about popular existing filetypes like .html or .sql. Would the same fix work for custom-defined file types? Let's say I want to use a new filetype with the extension .newft. If I create ~/.vim/ftplugin/newft.vim with the settings for this new type and load somefile.newft, would Vim automatically detect its type and load newft.vim?
I'm asking this because this is exactly what I'm doing and it's not working so far. I'd like to know whether this is an error or an expected behavior of Vim.
:h new-filetype outlines the different ways to add support for a new filetype.
I recommend method A which is as simple as writing the following in ~/.vim/ftdetect/newft.vim:
autocmd BufRead,BufNewFile *.newft set filetype=newft
and letting Vim deal with the rest.
Assuming you have filetype plugin on in your ~/.vimrc, the example above will make Vim try to source ~/.vim/ftplugin/newft.vim each time you read or create a buffer associated with a *.newft file or do :setfiletype newft/:set filetype=newft on an existing buffer.

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.

Manually and conditionally load plugins for 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.

Resources