tags:

views:

71

answers:

3

I turned on filetype plugin for some rails vim plugins I added, but a side effect of this seems to be that now autocommenting has been enabled in all filetypes (for instance, if I start a line with #, the next line, either by Enter in insert mode or O, etc. to enter insert mode, will also get a #).

I found a guide to disabling the auto-commenting formatoptions, and added the following to my .vimrc:

au FileType * setlocal formatoptions-=cro

However, I am still running into problems -- unless I explicitly :source .vimrc, (or enter the setlocal ... directly), it is not taking effect. I determined that this is the case because vim's ftplugins are overriding my options with their own.

I then found a second guide which talks about using an after ftplugin script to make changes after the ftplugin scripts have run, however their solution is to create symlinks for every single filetype in ~/.vim/after/ftplugin to a central file, and this seems to be kludgy to me.

Is there any way to create a generic after-ftplugin script or am I approaching this problem incorrectly? Any help would be appreciated.

+1  A: 

Using one of the various autocmd events to set the configuration option should work if you find the right one, but I'd start by running:

:verbose set formatoptions?

This will tell you where the option was set, which may make it easier to determine which autocmd to use. Alternatively, if you don't mind a bit of minor hacking, the way I'd probably do it is just to find out where it's set in the plugin and comment out that line (and make a note of it in case you ever upgrade the plugin). You could also contact the plugin's author and ask them to make it a configurable option.

For the available autocmd events, read this:

:help {event}
Al
Thanks for your answer, Al. I actually had checked through verbose before, and it returned `Last set from /usr/share/vim/vim72/ftplugin/ruby.vim`... so it's vim's own ftplugins that are setting the value last. I'm not sure how to use this information to determine which event to use though.
Daniel Vandersluis
+1  A: 

How about an "after" plugin? Create a file in ~/.vim/after/plugin/ called noAutoComments.vim (or whatever) and place your autocmd in that?

Edit:

The reason this works? I'm only guessing here, but I have a feeling that the autocmd in the ~/.vimrc file is getting removed by some other file (but before the "after" files are getting sourced).

I ended up removing my ~/.vim directory and replaced my ~/.vimrc with the following 3 lines:

filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro

With only these lines in my ~/.vimrc and no ~/.vim/ directory, the autocmd seems to work as expected (Vim 7.1).

For any file that I edit:

:verbose set formatoptions?
formatoptions=ql
      Last set from ~/.vimrc

I have yet to determine what file (plugin) is causing this issue however.

Curt Nelson
Even though I can see from `:scriptnames` that the ftplugins are still being loading after my after plugin, this seems to do the trick! Any reason you could think of why this would work?
Daniel Vandersluis
@Daniel: My only guess is that some other file is removing all of the `autocmd` s when it's loaded and that occurs before the sourcing of the "after" files. I'll update my answer with my findings.
Curt Nelson
@Curt: thanks for all your help, I seem to have figured out what is going on and have added an answer to explain it.
Daniel Vandersluis
A: 

I've done some more investigation and it seems that the location of my autocmd within my .vimrc file determines if formatoptions will be overridden by vim's ftplugins or not. Using vim --noplugin to disable all external plugins, I found the following results:

If my vimrc looks like:

au FileType * setl fo-=cro
filetype plugin indent on

The result of :verbose set fo? is:

formatoptions=croql
  Last set from /usr/share/vim/vim72/ftplugin/ruby.vim

However, if the lines in my vimrc are reversed:

filetype plugin indent on
au FileType * setl fo-=cro

The result of :verbose set fo? is:

formatoptions=ql
  Last set from ~/.vimrc

... which is the desired result. So it seems that the autocmd needs to be specified after filetype plugins are enabled.

Daniel Vandersluis