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:
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"