autocmd check filename in vim?

romeovs picture romeovs · May 15, 2011 · Viewed 8.8k times · Source

I want to write some autocmd's that check if the filename is this or that..

I've been trying:

autocmd VimEnter * if % is "preamble" | Explore | endif
autocmd VimEnter * if %=="preamble" | Explore | endif
autocmd VimEnter * if &filename == "preamble" | Explore | endif

but none of this works?

WHat should I do?

(this example is over-simplified, note that I'd also like to have an else statement).

Answer

abcd picture abcd · May 15, 2011

You should get the current file name as @%. For e.g., echo @% will give you the filename, whereas echo % will not. However, for actions on the file, use %, e.g. source %.

This should probably work for what you want to do:

autocmd VimEnter * if @% == 'preamble' | echo 'hello' | else | echo 'world' | endif