8

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

  • Related: [How can I get vim to stop putting comments in front of new lines?](http://vi.stackexchange.com/q/1983/467) at Vim SE – kenorb Feb 19 '15 at 18:39

5 Answers5

15

@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
Ory Band
  • 14,716
  • 14
  • 59
  • 66
14

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.

mMontu
  • 8,983
  • 4
  • 38
  • 53
  • 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. –  Apr 16 '13 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! –  Apr 16 '13 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 ", then you could understand what enables it. – mMontu Apr 16 '13 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? –  Apr 16 '13 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 http://vimhelp.appspot.com/vim_faq.txt.html#faq-26.6) – mMontu Apr 16 '13 at 12:55
9

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.

Paul
  • 13,042
  • 3
  • 41
  • 59
  • It wasn't that either :( Thanks though. –  Apr 16 '13 at 09:06
  • 1
    This solved my issue. Thanks. Note, you can also do this in a single line: `set formatoptions-=o formatoptions-=r` – phemmer Oct 02 '14 at 17:47
  • I found this solution is effective in neovim. You may see [my question](https://stackoverflow.com/q/76259118/21247237) for details. – Gorun May 16 '23 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