4

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?

2 Answers 2

7

suppress

The easiest would be to just suppress the error message via :silent! (note the !):

:au bufenter * silent! RainbowParenthesesToggle

check each time

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

avoid definition

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Also how would advice 3 - source the plugin first - change if I was using pathogen?
That stands for "exact match", see :help exists().
Pathogen extends the 'runtimepath', which :runtime uses. Should work the same, as long as the Pathogen stuff comes first.
When I copy the code here: :au bufenter * if exists(':RainbowParenthesesToggle') == 2 | RainbowParenthesesToggle | endif I get an error Trailing characters: RainbowParenthesesToggle | endif
Oh, the command cannot be directly put into a command sequence. Use exe 'RainbowParenthesesToggle' instead of RainbowParenthesesToggle.
1

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. :) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.