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"
Related
I want to write command like autocmd BufAdd * if &filetype != 'help' | echo 'foo', but it didn't work. I've tried different options: &l:filetype, &buftype, &l:buftype; tried with toggled filetype setting, but nothing changed. Global command autocmd BufAdd * echo 'bar' worked everywhere includes help pages, and the same time :echo &filetype on help pages returns help. What am I doing wrong?
Because,
While in BufAdd the current buffer can be (and quite probably is) different from the matched buffer;
While in BufAdd the matched buffer may (and quite probably will) not have any &filetype set yet.
Note that normally one should hook into some specific FileType with ~/.vim/after/ftplugin/xyz.vim instead.
How can I change a current working directory for a specific buffer and change it to a previous value for other buffers?
More precisely what I want to do is to have auto-commands for specific file types which will change a current working directory when a buffer containing the file type is active. And change it back when working with other buffers.
I had something like that in my vimrc:
autocmd BufEnter *.py :lcd%:p:h
autocmd BufLeave *.py :lcd-
The idea here is that I wanted to switch to file's directory when working on python files and switch to a previous directory when working on something else, e.g. text, vimL etc. Obviously, it didn't work. I also try using BufWinLeave, but it didn't change anything.
Can you point out what's wrong here? Or maybe there's an easier solution for that?
I think you could accomplish something similar with the following:
augroup CDRooter
autocmd!
autocmd BufWinEnter * if &filetype == 'python' | lcd %:p:h | else | lcd! | endif
augroup END
Note: I am not sure :lcd - will work well. Instead I use :lcd! to simply revert the window to using the global current working directory.
The basic idea is whenever you enter a window sniff the filetype and execute :lcd accordingly.
For more help see:
:h 'filetype'
:h BufWinEnter
:h :lcd
:h :augroup
:h :autocmd
:h :if
I have syntax highlighting enabled in .vimrc, but that makes loading certain files too long. So I need to disable (or, to be precise, not enable... enabling it and then disabling is not a solution) syntax highlighting for these files. I tried
au BufNewFile,BufRead !*.inc syntax enable
but that made just no syntax highlighting applied ever. The solution presented here does not work for me, since I can't make a distinction by filetype. I tried adapting to no avail, which might or might not be connected to the events needed for "syntax enable".
Thanks for any pointers!
The mentioned solution points to the right direction: Define an autocmd for all buffers, and then (instead of 'filetype'), match with the filename via expand('<afile>'):
au BufNewFile,BufRead * if expand('<afile>:e') !=? 'inc' | syntax enable | endif
Here, I've used your example of *.inc extensions in the condition. If you find the matching cumbersome and would rather use the autocmd syntax, you can do that with an intermediate buffer flag, too, using the fact that autocmds are executed in order of definition:
au BufNewFile,BufRead *.inc let b:isOmitSyntax = 1
au BufNewFile,BufRead * if ! exists('b:isOmitSyntax') | syntax enable | endif
If you want to show syntax only for .c files. Put
syntax off
autocmd! bufreadpost *.c syntax on
in your vimrc.
Also you can map a key for enabling syntax (Ctrl+s in this case)
nnoremap <C-S> :syntax on<CR>
In you question you want to disable syntax only for .inc file. Do it like this:
syntax on
autocmd! bufreadpost *.inc set syntax=off
To disable syntax highlighting for files with .inc extension, you can basically use:
syntax on
au BufNewFile,BufRead *.inc setlocal syntax=OFF
To disable it for multiple extensions, e.g. also for py:
au BufNewFile,BufRead *.{inc,py} setlocal syntax=OFF
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. :) )
I have the following lines in my .vimrc:
" UNIX fileformat
au BufRead,BufNewFile * set fileformats=unix,dos
au BufRead,BufNewFile * set fileformat=unix
These are to make any opened file using the unix file format.
It works well in almost all cases except for the help messages. If I type, let's say:
:h help
Vim first complains that:
"helphelp.txt" [readonly] 350L, 13662C Error detected while processing
BufRead Auto commands for "*": E21: Cannot make changes, 'modifiable'
is off: fileformat=unix
Obviously, I am trying to set the fileformat option on a non-modifiable buffer so this error is to be expected. However, what is the cleanest way to get rid of it without removing the feature for other files ?
Is there a way to conditionally apply/not apply autocommands for the help buffers ?
Thank you.
The 'fileformats' is a global setting, it should suffice to set it only once in your .vimrc:
set fileformats=unix,dos
The error will occur for any other unmodifiable file, not just help files. Therefore, it's best to make the setting conditional on the 'modifiable' buffer setting:
au BufRead,BufNewFile * if &l:modifiable | setlocal fileformat=unix | endif
(Alternatively, you could also just :silent! the error, but I regard the conditional as cleaner.)