I have following line in my vimrc-

autocmd BufRead,BufNewFile *.COB,*.PCO set filetype=cobol "force filetype as cobol for .COB and .PCO files

And I have put below lines in my custom plugin at the beginning (~/.vim/plugin/cobmods.vim)

if &ft != "cobol"

I don't want the plugin to load for other filetypes but this code doesn't work. I have tried putting the plugin inside ~/.vim/after/plugin so that it loads last but still it doesn't work.

Is there any different event that I am supposed to use?

Note: I have used below code inside the plugin and it works fine

if expand('%:e') != 'COB' && expand('%:e') != 'PCO'

But this doesn't look like the right way.

Also note: I can't change filetype.vim to include *.COB & *.PCO as I don't have the adequate rights as this resides in the installation location and not my home directory.

  • 1
    You don't have the rights to change ~/.vim/filetype.vim, in your home directory?
    – Rich
    Mar 30, 2018 at 13:20
  • @Rich filetype plugin resides in /usr/local/share/vim74/ so I cannot change it. I have created the ~/.vim folder manually to load my custom plugin.
    – Ankit Jain
    Mar 30, 2018 at 14:30
  • That's the system version. You can create your own file in your home directory.
    – Rich
    Mar 30, 2018 at 14:57

2 Answers 2


Files in plugin run during Vim startup (regardless of the file you're opening), before setting the file type.

Remove your check of the filetype option and move the file to:


I'd also recommend reading the contents of :help ftplugin, (in particular the part about setting b:did_ftplugin) and :help new-filetype.

  • There’s a cobol.vim in /usr/local/share/vim74/ftplugin too. Will putting a copy in my Home directory override the one in installed location or will it be the other way round. And I’ll read more about ftplugin as you mentioned. Thanks!
    – Ankit Jain
    Mar 30, 2018 at 14:32
  • :help new-filetype was helpful. I have now put the autocmd in filetype.vim in ~/.vim folder and moved my plugin to ~/.vim/ftplugin/cobol.vim and it worked as expected :)
    – Ankit Jain
    Apr 2, 2018 at 10:37

In addition to ft-plugin, you might check out

:h after-directory

It's a big hammer, but it works.

You shouldn't need to do this, however. And your test for ft == 'cobol' shouldn't be needed either, because if you put your ftplugin in ~/.vim/ftplugin/cobol.vim (as recommended above), only files of type 'cobol' will source it.

If you add the autocmd to set the file type for .cob files, it will be set correctly for files ending in .cob. But note that, in my plain out-o-the-box vim in current mint (ubuntu derivative) distribution, the cob extension will be detected without it.

You will need "already sourced" code though. Look at the first few lines of almost any file in $VIMRUNTIME/ftplugin.

  • Thanks, I have already put the plugin in after directory but it still doesn’t work as expected. Nevertheless I haven’t read the help page yet, maybe it will give me more insight.
    – Ankit Jain
    Apr 1, 2018 at 13:34

Your Answer

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

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