10

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

romeovs
  • 5,785
  • 9
  • 43
  • 74
  • 1
    I doubt whether VimEnter is the best event to trigger this autocmd. VimEnter happens once when you start a Vim instance and not after that. BufRead happens each time a buffer is opened, regardless of whether Vim was open already or not (and likewise with BufNewFile). – Herbert Sitz May 15 '11 at 20:27
  • Yeah I'm actually using `BufNew` to make it work – romeovs May 15 '11 at 20:39
  • Thank you all for your swift responses! – romeovs May 15 '11 at 20:40
  • 1
    This is what I'm using to change the color scheme except if the current buffer is a MiniBufExpl bufer and it seems to be working: `autocmd BufEnter * if bufname("%") !=? '-MiniBufExplorer-' | colorscheme default | endif` – User May 11 '14 at 20:38

4 Answers4

14

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
abcd
  • 41,765
  • 7
  • 81
  • 98
  • Is it possible to do more than one thing in 'if' part or 'else' part? e.g. `autocmd VimEnter * if @% == 'preamble' | echo 'hello1'; echo 'hello2' | else | echo 'world1'; echo 'world2' | endif ` – Rohan Ghige Oct 30 '19 at 13:13
7
autocmd BufRead,BufNewFile preamble Explore
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • Ok this works.. I would also like to have an `else` statement. Is this possible? – romeovs May 15 '11 at 16:54
  • @romeoevs: For effect of an `else` one way is to just create another autocmd that matches everything except 'preamble'. I think this would do it: `autocmd BufRead, BufNewFile [^\(preamble\)] function_here` (See my separate Answer for info on what you were doing wrong and how the file-slot in autocmd works.) – Herbert Sitz May 15 '11 at 17:28
5

There are two problems with the examples in the original post:

  1. Neither the % nor &filename will return the filename as you've used them.
    Look at the help for the expand() function to see how to get the filename: :h expand()

  2. You're ignoring the slot in autocmd where a file-matching-pattern would ordinarily be specified. The third slot in the autocmd (* in your versions) is actually a pattern to match filenames. See :h autocmd-patterns. It should work okay if you want to ignore the pattern slot, but if so, you've got to fix the problem in paragraph 1 above. E.g.,

    autocmd BufRead,BufNewFile * if expand('%') =~ "preamble" | Explore | endif

Community
  • 1
  • 1
Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54
3

Additional Info Using '@%' with autocmd

Some autocmd events do not support the @% named register to read the current file name. In these cases it is better to use <afile>:

expand('<afile>')
cmcginty
  • 113,384
  • 42
  • 163
  • 163