Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Vim error message "Not an editor command"

Tags:

vim

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?

like image 641
Kevin Burke Avatar asked Feb 14 '23 11:02

Kevin Burke


2 Answers

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.

like image 165
Ingo Karkat Avatar answered Feb 16 '23 01:02

Ingo Karkat


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

like image 28
Ben Klein Avatar answered Feb 16 '23 01:02

Ben Klein