3

Auto-comments are the most annoying "feature" of text editors and IDEs for me.

I've searched -- nay, scavenged -- high and low to figure out how to get rid of literally any semblance of automatic comment continuation, and for whatever reason it's proving impossible and incredibly annoying.

Right now, I have tried the following three lines, which works for most languages.

autocmd BufNewFile,BufRead,FileType * set formatoptions-=cro
autocmd BufNewFile,BufRead,FileType * setlocal formatoptions-=cro
au FileType c,cpp setlocal comments-=:// comments+=f://

But whenever I open a file that isn't considered C++ at first (i.e. not the correct extension) and use setf cpp, all auto-comments seem to come back to haunt me.

Even sometimes opening a known-to-be-C++ file, this still happens. I'm not sure what the cause is, but it's aggravating.

How on Earth can I tell vim that I really truly do not want auto-comments, ever, under any circumstances? I feel like I've been plagued by this for years.

6
  • 3
    Just to eliminate the obvious first: we are sure that you don't have a plugin which re-enables your autocomments? If you start vim with a vimrc containing only the 3 lines in your questions you still observe the same problem?
    – statox
    Commented Jan 25, 2017 at 7:53
  • @statox Yeah, I tried that already. This has definitely been a problem since before I knew plugins were even a thing (years ago). Always good to check, though. Commented Jan 25, 2017 at 10:07
  • The answer is simple: do not enable filetype plugins. Commented Jan 25, 2017 at 11:39
  • @ChristianBrabandt doesn't that disable coloring as well? Commented Jan 25, 2017 at 12:29
  • 1
    No, however in C and C++ cases it'll break matchpair, plugins that rely on 'commentstrings' to (un)comment blocks of code, and it'll completely disable plugins that define what they provide in ftplugins. For instance, in lh-cpp I define the doxygenating command :DOX in an ftplugin. I understand that some default settings don't suit us, however without ftplugins, vim would be quite crippled for programming tasks. They were the huge improvement of Vim 6. Commented Jan 25, 2017 at 18:17

1 Answer 1

6

The culprit is $VIMRUNTIME/ftplugin/c.vim, and likely all other standard ft plugins. If you want everything they define but the setting for 'formatoptions', I don't see any simple solution. (Just in case, $VIMRUNTIME is set within vim)

May be, you could listen for OptionSet to prevent inserting cro in &fo. But beware of possible infinite loops. I've never tried it.

BTW, I would have left 'comments' alone. This option could be used by plugins that toggle comments on blocks, or that generate documentation -- I actually use it in mu-template to insert license captions as comments, which ever the current language is.

5
  • 2
    You are an absolute god. autocmd BufNewFile,BufRead,FileType,OptionSet * set formatoptions-=cro and autocmd BufNewFile,BufRead,FileType,OptionSet * setlocal formatoptions-=cro did it. The struggle is finally over. I feel like I can develop again. The bonds of magic text editors have been freed and my fingers feel liberty once again. Commented Jan 25, 2017 at 10:05
  • 1
    Any chance you could elaborate? I had a quick look in my copy (Vim 7.4) of c.vim and can't see anything that's not easily overridden (and I haven't ever had any problems with setting formatoptions myself, so I don't really understand what's going wrong.) Why is listening for OptionSet necessary in this case?
    – Rich
    Commented Jan 25, 2017 at 10:18
  • 1
    @Rich. It's about not overriding every possible standard ftplugins. Yes this is indeed possible, but this requires a lot of work. In a normal scenario, we code in 2-3 programming language and we can then override 2-3 ftplugins, copy everything we like (and the b:did_filetype guard), but the formatoption, or even better, let the standard ftplugin do its job, and then cancel in $HOME/.vim/after/ftplugin/ft1.vim the setting on &fo. We can also in two lines override one standard setting for all filetypes if we really don't like it. Commented Jan 25, 2017 at 12:16
  • 1
    @Qix, I've cheated. Yesterday I was looking for a ShellCmdPre event (which doesn't exist). In the process, I've re^42-discovered OptionSet. Commented Jan 25, 2017 at 12:18
  • 1
    OptionSet has been included in a Vim 7.4 patch. It might not be available in all versions. Commented Jan 25, 2017 at 17:34

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.