From d0c67ec987d663a0d6015f55db72307e958b668d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 17 Apr 2016 22:45:46 +0900 Subject: [PATCH] Conditional activation --- faq.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/faq.md b/faq.md index f27901a..15a59ca 100644 --- a/faq.md +++ b/faq.md @@ -14,20 +14,45 @@ endif (If you're behind an HTTP proxy, you may need to add `--insecure` option to the curl command. In that case, you also need to set `$GIT_SSL_NO_VERIFY` to true.) -### Loading plugins manually +### Conditional activation -With `on` and `for` options, vim-plug allows you to defer loading of plugins. But if you want a plugin to be loaded on an event that is not supported by vim-plug, you can set `on` or `for` option to an empty list, and use `plug#load(names...)` function later to load the plugin manually. The following example will load [ultisnips](https://github.com/SirVer/ultisnips) and [YouCompleteMe](https://github.com/Valloric/YouCompleteMe) first time you enter insert mode. +Use plain "if" statement to conditionally activate plugins: ```vim -" Load on nothing -Plug 'SirVer/ultisnips', { 'on': [] } -Plug 'Valloric/YouCompleteMe', { 'on': [] } +if has('mac') + Plug 'junegunn/vim-xmark' +endif +``` -augroup load_us_ycm - autocmd! - autocmd InsertEnter * call plug#load('ultisnips', 'YouCompleteMe') - \| call youcompleteme#Enable() | autocmd! load_us_ycm -augroup END +The caveat is that when the condition is not met, `PlugClean` will try to remove the plugin. This can be problematic if you shar the same configuration across terminal Vim, GVim, and Neovim. + +```vim +" When started with plain Vim, the plugin is not registered +" and PlugClean will try to remove it +if has('nvim') + Plug 'benekastah/neomake' +endif +``` + +Alternatively, you can pass an empty `on` or `for` option so that the plugin is registered but not loaded by default depending on the condition. + +```vim +Plug 'benekastah/neomake', has('nvim') ? {} : { 'on': [] } +``` + +A helper function can improve the readability. + +```vim +function! Cond(cond, ...) + let opts = get(a:000, 0, {}) + return a:cond ? opts : extend(opts, { 'on': [] }) +endfunction + +" Looks better +Plug 'benekastah/neomake', Cond(has('nvim')) + +" With other options +Plug 'benekastah/neomake', Cond(has('nvim'), { 'on': 'Neomake' }) ``` ### Gist as plugin @@ -102,6 +127,22 @@ call plug#begin('~/.vim/bundle') call plug#begin('~/vimfiles/bundle') ``` +### Loading plugins manually + +With `on` and `for` options, vim-plug allows you to defer loading of plugins. But if you want a plugin to be loaded on an event that is not supported by vim-plug, you can set `on` or `for` option to an empty list, and use `plug#load(names...)` function later to load the plugin manually. The following example will load [ultisnips](https://github.com/SirVer/ultisnips) and [YouCompleteMe](https://github.com/Valloric/YouCompleteMe) first time you enter insert mode. + +```vim +" Load on nothing +Plug 'SirVer/ultisnips', { 'on': [] } +Plug 'Valloric/YouCompleteMe', { 'on': [] } + +augroup load_us_ycm + autocmd! + autocmd InsertEnter * call plug#load('ultisnips', 'YouCompleteMe') + \| call youcompleteme#Enable() | autocmd! load_us_ycm +augroup END +``` + ## FAQ ### Does vim-plug generate help tags for my plugins?