This ~/.vimrc excerpt;

if has('syntastic')
    let g:syntastic_sh_shellcheck_args="-x"

is always false. What is the correct condition to check?

I also tried

if exists('*syntastic*')

Without conditional code in vimrc it becomes non-portable and I get errors like this;

> vim .vimrc
Error detected while processing /root/.vimrc:
line    7:
E185: Cannot find color scheme 'PaperColor'
line   15:
E117: Unknown function: pathogen#infect
E15: Invalid expression: pathogen#infect()
Press ENTER or type command to continue

from a .vimrc like this;

syntax enable
set number
set laststatus=2
set background=dark
set foldmethod=syntax
let g:PaperColor_Theme_Options = {'theme':{'default.dark':{'transparent_background':1}}}
colorscheme PaperColor
if has('cmdline_info')
    set ruler
    set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%)
    set showcmd
    set hlsearch

execute pathogen#infect()
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
autocmd VimEnter * redraw!
let g:syntastic_check_on_wq = 0
let g:syntastic_java_javac_config_file_enabled = 1
let g:syntastic_sh_shellcheck_args="-x"
  • has is for features; perhaps you wanted exists ? – D. Ben Knoble Dec 17 '18 at 20:19
  • If you want to test whether syntastic is installed: if globpath(&runtimepath, 'plugin/syntastic.vim', 1) !=# '' - but that won't work with plugin managers that use lazy loading. – lcd047 Jan 17 '19 at 14:33

The has() always checks for features compiled into Vim (e.g. mostly what you can see in the output of :version with either a + or - prepended). It won't work to test for loaded plugins.

What would work, is checking if the script loading guards, that usually every well-defined plugin has. For syntastic this would be the variable g:loaded_syntastic_plugin, so you would need to check:

if get(g:, 'loaded_syntastic_plugin', 0)
    let g:syntastic_sh_shellcheck_args="-x"

However, there is one gotcha. Usually the vimrc file is loaded before all plugins, to be able to set plugin specific configuration variable before the plugin is active. So this means, even for a Vim that has syntastic installed, the above check wouldn't work (since the plugin hasn't yet set the corresponding loaded_ variable yet).

There are a couple of ways around it, usually globbing your runtime path and checking for the syntastic plugin manually. That could be slow however if your vim runtime is on a network path for example.

You could also put the check into an VimEnter autocommand which will then evaluate once Vim has started up correctly.

However, I would suggest, getting rid of the check altogether, and simply set the variable whether syntastic is loaded or not. After all, simply setting a global variable won't hurt even if you do not have syntastic installed.

| improve this answer | |
  • what vim version are you using? – Christian Brabandt Dec 18 '18 at 13:19
  • Additionally, the ~/.vim/after/plugin directory can be used with this sort of conditional, since it is loaded after plugins. – D. Ben Knoble Dec 18 '18 at 17:38
  • @user1133275 okay, so that is a totally different question and well out of scope for the current answer. You want to conditionally call functions, depending on whether a plugin is installed. You need to check the presence of a plugin and you can do this using the globpath() function. – Christian Brabandt Dec 19 '18 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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