Vim test if options are valid before setting

I use my .vimrc on a number of different machines, which all have significantly different versions of vim. Most versions boot up fine, but one of them has reports this (doctored) list of errors on startup:

Error detected while processing ~/.vimrc:
Undefined variable: pathogen
Invalid expression: pathogen#infect()
Invalid :syntax subcommand: enable
Unknown function: executable
Invalid expression: executable('xclip')
:else without :if: else
:endif without :if: endif
Invalid argument: indent on
Invalid argument: plugin on
Invalid argument: nrformats=alpha,hex,octal
Illegal arguments: fromstart
Not an editor command: colorscheme zobean
Unknown option: foldenable
Unknown option: foldcolumn=0
Unknown option: cursorline
Unknown option: winminwidth=0
Ambiguous mapping, conflicts with "^V<silent>"
Ambiguous mapping, conflicts with "^V<leader>"
Ambiguous mapping, conflicts with "j"

I know I can use if has('mouse') to test if mouse support is enabled. Is there a similar test I can do for the other commands, like pathogen, and settings and options that are commonly missing?

Note, I do not have permission to install programs on all of these machines, so installing the latest vim is not a valid option.

I intend to only maintain a single .vimrc for all machines, that will appropriately tailor the options to what is available.

Appendix: vimrc content

I will not include my entire .vimrc, as it about 250 lines long, but below is representative lines and sections causing each of the errors listed above:

execute pathogen#infect()
syntax enable
if has('win32')
    ...
elseif executable('xclip')
    ...
else
    ...
endif
filetype indent on
filetype plugin on
set nrformats=alpha,hex,octal
syn sync fromstart
colorscheme zobean
set foldenable foldnestmax=5 foldmethod=indent
set foldcolumn=0
set cursorline
set winminwidth=0
vnoremap < <gv
nnoremap <leader>~ vwb<esc>~
vnoremap jk <esc>

Answers 1

  • if exists('*pathogen#infect')
    " The function pathogen#infect() exists
    else
    " The function pathogen#infect() does not exist
    endif
    

    Use :help exists() for more infomation. In addition to variables, exists() is also able to test for &options, *functions, :commands, #event autocommands.


Related Questions