9

I have the following lines in my vimrc:

" Don't add the comment prefix when I hit enter or o/O on a comment line.
set formatoptions-=or

It used to work at some point. I don't know what I did but it doesn't anymore, I still get comments when creating newlines. What could have disabled it? Here's my vimrc: http://pastebin.com/kVWWeQWW

1

5 Answers 5

16

@mm2703 's solution didn't work for me, especially for java files, but this change did. I also wrapped it in an augroup, statement so resourcing .vimrc won't re-register the autocmd:

augroup Format-Options
    autocmd!
    autocmd BufEnter * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

    " This can be done as well instead of the previous line, for setting formatoptions as you choose:
    autocmd BufEnter * setlocal formatoptions=crqn2l1j
augroup END
1
  • Ugh, this finally did it. I think I tried 4 answers before getting here. Thank you. Commented Sep 6, 2016 at 3:07
15

If you found that the flag o is being inserted back in formatoptions after startup, you should find out why this is happening and fix it. This seems cleaner than always executing an autocmd, even after the option is removed.

You could check where the option is set issuing with following commands:

:5verbose set fo?
:5verbose setl fo?

Edit:

If your problem is with a ftplugin file that lies on Vim directory ($VIMRUNTIME/ftplugin) you shouldn't change that file, because that change would be undone when Vim is updated. The proper way to change it is in your after directory, as explained in :h after-directory.

Supposing that the problem occurs with c filetype, create file ~/.vim/after/ftplugin/c.vim containing your setlocal formatoptions-= commands.

6
  • It is added every time a file is opened. How can I know what enables it? There's nothing else in my vimrc changing the formatoptions.
    – user347284
    Commented Apr 16, 2013 at 11:54
  • Ohhhhhhhh. Yeah, I had. But I hadn't removed the autocmd line. I've just done it and this time your command led me to the culprit which is ftplugin. Thanks!
    – user347284
    Commented Apr 16, 2013 at 12:00
  • The configuration can be changed by many other places beyond your .vimrc -- on ftplugins, for instance. If you disable your fix based on autocmds and enter in a buffer where :set formatoptions? displays the o option, issuing the commads above should display something like "Last set from <filename>", then you could understand what enables it.
    – mMontu
    Commented Apr 16, 2013 at 12:01
  • Edit: actually, I had a vimOutliner file in my ~/.vim/ftplugin folder so the first thing I did was to uninstall it. But the problem remained because the problem indeed came from a $VIMRUNTIME/ftplugin. Since the problem happens in almost every code file I'm editing maybe the autocmd is the best choice after all? What do you think?
    – user347284
    Commented Apr 16, 2013 at 12:17
  • If you change $VIMRUNTIME/ftplugin your changes will be lost if Vim is updated and/or if you change to another machine, even if you copy your .vim and .vimrc. The solution I proposed on the edit is better because it avoid these problems, and can be used to include further customization to each filetype in the recommended way (see :h after-directory and vimhelp.appspot.com/vim_faq.txt.html#faq-26.6)
    – mMontu
    Commented Apr 16, 2013 at 12:55
10

If your 'formatoptions' contains options in different order, like ro, then -=or won't work. Try

set formatoptions-=o
set formatoptions-=r

From help remove-option-flags:

Note that you should add or remove one flag at a time. If 'guioptions' has the value "ab", using "set guioptions-=ba" won't work, because the string "ba" doesn't appear.

3
  • It wasn't that either :( Thanks though.
    – user347284
    Commented Apr 16, 2013 at 9:06
  • 1
    This solved my issue. Thanks. Note, you can also do this in a single line: set formatoptions-=o formatoptions-=r
    – phemmer
    Commented Oct 2, 2014 at 17:47
  • I found this solution is effective in neovim. You may see my question for details.
    – Gorun
    Commented May 16, 2023 at 14:20
6

I realized with :set formatoptions? that although the o flag was disabled at startup, it came back when opening a file. This in my vimrc fixed it:

" Don't add the comment prefix when I hit enter or o/O on a comment line.
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
0

Thanks, this worked for me - but I'm disabling annoying formatting features...

autocmd!
autocmd BufEnter * setlocal formatoptions-=c formatoptions-=q formatoptions-=n formatoptions-=r formatoptions-=o formatoptions-=l

Seems like vimrc just ignores the combined syntax, eg. formatoptions-=cqn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.