2

I am currently cleaning up my .vimrc. I learned that I can spread out filetype specific commands into separate files in the .vim/vimfiles directory by putting them into ftplugin and after/ftplugin directory, respectively.

So, I put all the filetype specific options (e.g. expandtab, spell...) there that I previously set with autocommands.

But I also have variables in my .vimrc that are specific to filetype. E.g. for C I set (or let) the following:

let g:load_doxygen_syntax=1
let c_space_errors=1
unlet c_comment_strings
let c_curly_error=1
let c_syntax_for_h=1
let g:doxygen_javadoc_autobrief=0
let g:doxygen_enhanced_color=1

I now put this into the after/ftplugin/c.vim file and it seems to work like before.

Are there any consequences of this that I did not foresee? Now those variables are set every time I open a C file instead of once after startup. May there be any negative affect due to that?

2
  • 1
    "Are there any consequences of this that I did not foresee?" - wait until the problem surfaces, then fix it. FWIW, I did what you did a couple of years ago and noticed my Vim experience improved because I had eliminated unwanted side effects.
    – Friedrich
    Commented Apr 6 at 20:32
  • Is there still something open in your question? How can we help you further? Otherwise maybe could you accept one of the solution using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Apr 9 at 21:00

3 Answers 3

1

The ftplugin intention is to replace the autocmd FileType calls.

The corresponding file is executed after the filetype is set and before the corresponding distribution file is executed.

It is not too late to set the global variable that influence the distribution file.

Remark: The after/ftplugin intention is to override the distribution behavior.

For the variables that control/influence the plugins I can imagine situations where it would not be equivalent (the plugin initialisation could happen before the filetype of a buffer is set)

1

Filetype plugins are meant to set buffer local stuff. All your variables are global. IMO, It doesn't make sense to set, and reset, and reset them again and again every time you open a new C file.

Even if it doesn't make any difference and if it still behaves as expected (loaded before the syntax files), I would leave them in the .vimrc -- in a folded section.

Use the ftplugins to store what you set from filetype related autocommands. And autoload plugins to store functions that don't need to be executed when vim starts

0

Filetype plugins, whether they are from $VIMRUNTIME/ftplugin, ~/.vim/ftplugin, or ~/.vim/after/ftplugin, are sourced before any syntax script. The "options" you listed in your question are all syntax-related so it should be safe to relocate them in ~/.vim/after/ftplugin.

But it is entirely possible that you stumble upon a rogue filetype plugin that wrongly does something like runtime syntax/foo.vim, in which case your preferences may be totally or partially ignored.

For that (admittedly a little bit paranoid) reason, I would keep them in my vimrc. YMMV.

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.