vimif-statementautocmd

autocmd check filename in vim?


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


Solution

  • 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