Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Another reason this might not be taking effect...

From :he :set-=:

            When the option is a list of flags, {value} must be
            exactly as they appear in the option.  Remove flags
            one by one to avoid problems.

I have

    " Turn off auto-commenting
    au FileType * setlocal formatoptions-=c
    au FileType * setlocal formatoptions-=r
    au FileType * setlocal formatoptions-=o

because I've run into this.

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}

I have tried solutions proposed by many, but none of them worked for me, but I found one very simple workaround, namely, in your ~/.bash_aliases:

    # vim without auto comment
    alias vi="vi +'set fo-=cro'"

I was struggling with this issue and I finally works with the following lines:

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

I think the key here is that the autocmd is place after the filetype plugin on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top