How to avoid Vim error message "Not an editor command" - 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?

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.

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

Related

vimdiff: start in second split?

When using vimdiff, I'd like to start in the second pane (my changes), instead of the first.
I've tried adding this to my .vimrc, but it doesn't seem to work. Is there a different event I should be hooking?
if &diff
autocmd BufWinEnter * winc l
endif
The following worked for me
if &diff
autocmd VimEnter * normal ^W^L
endif
Note here that ^W and ^L are single characters. You can enter them using ctrlvctrlwctrlvctrll

Operatorcoloring plugin script doesn't work after switching colorscheme

In vim I've written a plugin in order to enable operator coloring,
file: ~/.vim/sources/operatorColoring.vim
if has('autocmd')
autocmd BufRead,VimEnter * syn match parens /[(){}]/ | hi parens ctermfg=082
autocmd BufRead,VimEnter * syn match sqparens /[\[\]]/ | hi sqparens ctermfg=087
autocmd BufRead,VimEnter * syn match operatorsA /[=+%&*^;<>-]/ | hi operatorsA ctermfg=063
autocmd BufRead,VimEnter * syn match operatorsB /[,.;!]/ | hi operatorsB ctermfg=063
autocmd BufRead,VimEnter * syn match operatorsC /[:]/ | hi operatorsC ctermfg=196
endif
I enable this, depending on the file type. For example for python:
file: ~/.vim/ftplugin/python.vim
source ~/.vim/sources/operatorColoring.vim
This principle works perfect, however it breaks when I switch the colorscheme by issuing the command :colorscheme mycolorschme
I believe filetype plugins are loaded automatically when a colorscheme is switched. when I issue the command :filetype the output is:
filetype detection:ON plugin:ON indent:ON
Just in case, I've also tried to issue the command filetype=python, still operator-coloring doesnt work. I've also tried to manually source the operatorColoring script by issuing the command source ~/.vim/sources/operatorColoring.vim, nothing happens when I do this. I'm a bit lost here, hope somebody can help.
I've found the solution for this behaviour, simply typing :e will tell vim to reload all buffer-specific files (more info here), hence in this use case it will reload the operatorColoring script which solves the problem.

Vim plugin : Rainbow parentheses using tab

I'm using vim 7.3 and Rainbow Parentheses plugin. When opening multiple tabs with vim -p file1 file2 or with vim -S session.vim, or even with tabnew file or any other method, my parenthesis are colored in only one file.
I just put this into my .vimrc : au VimEnter * RainbowParenthesesToggle
as said here. I tried to use :RainbowParenthesesToggle on the other tabs once opened but it only toggles in the parenthesis-activated tab.
What should I do to make things work in all tabs ?
I made it work by adding the same instructions as here in my .vimrc, thanks to FDinoff. I replaced the last instruction to make it work using tab, as I intended first.
function! Config_Rainbow()
call rainbow_parentheses#load(0)
call rainbow_parentheses#load(1)
call rainbow_parentheses#load(2)
endfunction
function! Load_Rainbow()
call rainbow_parentheses#activate()
endfunction
augroup TastetheRainbow
autocmd!
autocmd Syntax * call Config_Rainbow()
autocmd VimEnter,BufRead,BufWinEnter,BufNewFile * call Load_Rainbow()
augroup END
The VimEnter flag on the autocommand tells vim to perform the command specified (in this case RainbowParenthesesToggle only when starting the editor, which is in your case when you open the first file.
If you want to extend the functionality to everytime you load a buffer you should do something like:
autocmd BufRead,BufNewFile * RainbowParenthesesToggle

Setting buffer specific variables in Vim

I'm trying to set a buffer-local variable to a global variable if the local variable is not defined using the following autocmd
au BufEnter * if !exists('b:Variable') | let b:Variable = g:Variable | endif
However, b:Variable is not getting defined. What am I doing incorrect here?
EDIT: To rephrase/clarify, b:Variable is being used in the file. The error is that b:Variable is not defined.
First of all, your autocommand is correct. It should work.
I see some reasons why it could fail:
g:Variable is not defined
Events are disabled, see :help eventignore (this is unlikely)
The autocommand-feature is not supported by your vim version (this is unlikely too). :version must list +autocmd.
My favorite: The autocommand is not sourced. Are you sure that your autocommand is active?
What does
:verbose autocmd BufEnter
say? If your autocommand is not listed, it is not active.
Or try something that is simpler and that gives direct feedback to see if autocommands with BufEnter generally work. For example
:au BufEnter * echom "Buffer Entered"

In .vimrc, how to test whether any filename arguments are given to Vim at invocation?

The answer to the question
Using vim Sessions Only With GUI? suggests using
au VimLeave * mksession! ~/.gvimsession
au VimEnter * source ~/.gvimsession
My problem is when I start Vim, say, by issuing $ gvim test.html, the test.html file is loaded into a buffer that is not shown.
How can I test if arguments where passed and not execute the au VimEnter in such a case?
Or, alternatively, how can I switch to the buffer holding the file with the given file name.
One can test whether there are any command-line arguments (using the
argc() function) and load the previously saved session only when
there are not any:
:autocmd VimEnter * if argc() == 0 | source ~/.gvimsession | endif

Resources