1

I have to edit a bunch of *.txt files for which I want to define a specific filetype. So, in my vimrc, I add a

autocmd! BufReadPost bunchOfFiles/*.txt :set ft=bunchOfFilesFT

This reads ftplugin/bunchOfFilesFT.vim as wanted. The problem is that it also reads ftplugin/text.vim since there is an according autocmd that was defined in filetype.vim that comes with a vim installation.

Since my bunchOfFilesFT filetype should be completely independent of the text filetype (despite its suffix), I'd rather have vim not load ftplugin/text.vim. Is this somehow possible?

  • Does your ftplugin/text.vim have a if exists("b:did_ftplugin") section at the beginning to finish early if some ftplugin has already been did? And do you set b:did_ftplugin in your ftplugin/bunchofFilesFT.vim? – jjaderberg Aug 12 '15 at 8:15
  • No, I didn't. I guess this is the canonical way to proceed for what I want? – René Nyffenegger Aug 12 '15 at 11:20
1

It is a good idea to begin plugins, colorschemes, etc., by checking that it has not already been run for the current buffer. This prevents running the plugin again if it's not necessary and it gives the user the ability to easily disable it. For an ftplugin this usually looks like

if exists("b:did_ftplugin")
  finish
endif
let b:did_ftplugin = 1

If your text.vim ftplugin does this, you can prevent it loading by setting the b:did_ftplugin variable before text.vim is loaded.

For example, Vim's ftplugin/vim.vim does a check like that. So I put the above snippet in ftplugin/jjvim.vim and

autocmd! BufReadPost *.vim :set ft=jjvim

in my vimrc. Then I start a new vim session and edit ftplugin/jjvim.vim again, and now only that ftplugin is loaded, not Vims vim.vim.

So make sure that your text.vim contains a check like above, and make sure you set b:did_ftplugin before text.vim is loaded, for instance from your bunchofFilesFT.vim, and you should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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