Let's say I have the following in my .vimrc:
au bufenter * RainbowParenthesesToggle
However I'm on a unfamiliar machine and I haven't installed all of my plugins yet. This means when I start up Vim I get this error message:
E492: Not an editor command: RainbowParenthesesToggle
How can I guard against this, or what if statement do I want to wrap these calls in to avoid getting this error message when I start Vim?
The easiest would be to just suppress the error message via :silent!
(note the !
):
:au bufenter * silent! RainbowParenthesesToggle
It's cleaner (especially for an autocmd that runs on each BufEnter
) to avoid the call. The existence of a command can be checked with exists(':RainbowParenthesesToggle') == 2
.
:au bufenter * if exists(':RainbowParenthesesToggle') == 2 | RainbowParenthesesToggle | endif
It would be best to check only once, and avoid defining the autocmd at all. The problem is that your ~/.vimrc
is sourced before the plugins! There are two ways around this:
1) Explicitly source the plugin before the check:
runtime! plugin/rainbowparentheses.vim
if exists(':RainbowParenthesesToggle') == 2
au bufenter * RainbowParenthesesToggle
endif
2) Move the definition and conditional to a location that is sourced after the plugins. ~/.vim/after/plugin/rainbowparentheses.vim
would be a good place for this.
You can check for the command using exists()
:
au bufenter * if exists(":RainbowParenthesesToggle") | RainbowParenthesesToggle | endif
(I have no such command defined myself, so I can verify that this works. :) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With