I'm trying to set up an autocommand so that new, unnamed buffers are created with ft=markdown
. Someone on SO suggested the following:
autocmd BufEnter * if &filetype == "" | setlocal ft=markdown | endif
which works great, but with which I have the following gripes:
- this will
set ft=markdown
on named buffers, too (e.g.,:e foo
), and - this will run every time I switch to (enter) the buffer, not just when it's first created — not a problem in practice, but not what I want to do in principle.
I've been working through the problem myself, but I can only get halfway there.
Success: the right autocmd {pat}
I've figured out that I can specify unnamed buffers by using a pair of empty curly braces ({}
) in place of a filename pattern. That is,
autocmd VimEnter {} setlocal ft=markdown
will run on $ vim
, but not on $ vim foo
.
Failure: the right autocmd {event}
So now I'm looking for the right autocmd event: one that is triggered on :enew
or any other time a blank, unnamed buffer is added to the buffer list. I've tried:
BufAdd
BufCreate
BufNew
but none of these are triggered on :enew
. I've tested this with the following autocommands:
autocmd BufAdd * let b:foo = 'foo'
autocmd BufCreate * let b:foo = 'foo'
autocmd BufNew * let b:foo = 'foo'
When I edit an existing file, :echo exists('b:foo')
returns 1. When I :enew
, it returns 0.
(To be clear, BufEnter
still works, but I'm looking for one that only runs once.)
Is there an appropriate autocmd here, or should I file a bug?