I have below autocmd in my cobol.vim ftplugin file in ~/.vim/ftplugin/

augroup cobmods
    autocmd BufNewFile * call s:CleanStart()
augroup END

This doesn't work when I open a file using vim filename.cob.

However, if I put the same in my .vimrc, it works.

Anybody with any insights on how I can change this autocmd to work from cobol.vim as I really don't want to put cobol related mods in vimrc.

Also looks like, by the time program (vim) flow reaches cobol.vim, BufNewFile event is no longer valid.

  • 2
    Your last sentence is correct. BTW: what exactly you want the function s:CleanStart() to be doing? Jun 7, 2018 at 14:56
  • Whenever I open an empty (new) cobol file, CleanStart would create a skeleton of cobol program.
    – Ankit Jain
    Jun 7, 2018 at 15:28
  • 1
    In that case, the BufNewFile triggers to late (because it will be only defined after initially the FileType event for cobol has been triggered and by that time the BufNewFile event has already been triggered). So I would create a similar autocommand in my vimrc with something like BufNewFile *.cobol :call CobolStart() Jun 7, 2018 at 16:08
  • I don't want to put any cobol related mods in vimrc, so I modified my function CleanStart to include if !empty(glob(fnameescape(expand('%:p')))) condition and changed to autocmd to VimEnter *.COB and now it works as expected. Although I'm not sure if this is the most elegant way.
    – Ankit Jain
    Jun 8, 2018 at 6:54

1 Answer 1


Problem was already diagnosed in the comments, and your current solution won't work if you try to start editing a file from within vim. If that's a problem, try this:

" This goes directly in ~/.vim/ftplugin/cobol.vim - no autocommands needed
if line('$') == 1 && getline(1) == ''
  call s:CleanStart()

(I tried adding this as a comment but I can't format newlines in comments as far as I know)

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.