I write this line to the ~/.vim/ftplugin/c.vim file, but nothing happens when I create a new C file:

autocmd BufNewFile *.c read ~/skeleton/c

Afterwards, I added this line to $MYVIMRC:

autocmd BufNewFile *.c echo 123

Then the skeleton started to be added to new C files.


  • Thank to the edit
    – zhzhy
    Jan 14, 2020 at 4:56

1 Answer 1


Well, actually it works, but probably not the way you think.

  1. Autocommands are invoked in sequence. It's BufNewFile and/or BufRead that are used by ftplugin to set filetype which results in FileType autocommand which, in turn, does source all your stuff. So you've already missed BufNewFile for that particualr buffer.

  2. Nonetheless, autocmd without <buffer> specification is global, so you've set it up for later. And did it wrong: each time FileType c is triggered, you add another instance of read for later buffers.

So your options are:

  1. Add it to vimrc:

    augroup MySkeletons | au!
        autocmd BufNewFile *.c read ~/skeleton/c
    augroup end
  2. Manually check if the current buffer is empty:

    In ~/.vim/after/ftplugin/c.vim:

    if line('$') == 1 && empty(getline(1))
        read ~/skeleton/c
  • @eyalkarni It's possible but it's precisely what ftplugin does. So that's the reason why one never needs to trap BufRead himself (except to detect filetype).
    – Matt
    Jan 28, 2020 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.