67

I'm editing my .vimrc file, and I am including comments. When I have a comment like this:

" example comment

And then hit enter at the end of the line after finishing the comment, the next line is automatically set up as a comment (the quotes are automatically inserted):

" example comment
" 

How can I stop this behavior?

3 Answers 3

89

This is controlled with the formatoptions setting; from :help fo-table:

You can use the 'formatoptions' option to influence how Vim formats text. 'formatoptions' is a string that can contain any of the letters below. The default setting is tcq. You can separate the option letters with commas for readability.

Note that the statement about the "defaults" is somewhat misleading, since many filetypes change the formatoptions to best suit the file type; for example in /usr/share/vim/vim74/ftplugin/vim.vim:

" Set 'formatoptions' to break comment lines but not other lines,
" and insert the comment leader when hitting <CR> or using "o".
setlocal fo-=t fo+=croql

You can view the current formatoptions with:

:set fo?
  formatoptions=jcroql

And see where they were set like so:

:verbose set fo?
  formatoptions=jcroql
      Last set from /usr/share/vim/vim74/ftplugin/vim.vim

In this case, you want to remove the r flag, but perhaps also the c and o flags:

r       Automatically insert the current comment leader after hitting
        <Enter> in Insert mode.
c       Auto-wrap comments using textwidth, inserting the current comment
        leader automatically.
o       Automatically insert the current comment leader after hitting 'o' or
        'O' in Normal mode.

This can be done like so:

:set formatoptions-=r formatoptions-=c formatoptions-=o

Note that using :set formatoptions-=cro won't work as expected (since it's a string, it will look for the string cro, in that order, which often won't work.).

To set the changes only for the current buffer, use :setlocal instead of :set. If you want to always have these options, it's probably best to use an autocmd in your vimrc; for example:

au FileType vim setlocal fo-=c fo-=r fo-=o

This will set the options only for the 'vim' filetype, and won't interfere with other filetypes.

If you want to always set it, use:

au FileType * set fo-=c fo-=r fo-=o

Just using set fo-=cro won't work since many filetypes set/expand formatoption (like shown above); the FileType autocmd is executed after the filetype files have loaded.

3
  • 1
    Confirmed. Also, it's answered here: stackoverflow.com/questions/16030639/vim-formatoptions-or
    – Melon
    Commented Feb 19, 2015 at 16:48
  • 1
    Adding au FileType * set fo-=o to my .vimrc didn't work. I press o in a commented line and it still comments it too.
    – User
    Commented Mar 30, 2018 at 19:39
  • This has been bugging me for a long time. Default formatoptions were wrapping my txt file. I was breaking my head around textwidth and wrapmargin but nothing worked. Even downgraded my vim from 8.1 to 8.0 thinking that it's a bug. This solved my issue, thanks.
    – anishjp
    Commented Jul 10, 2019 at 11:01
4

Add this in your vimrc:

au BufEnter * set fo-=c fo-=r fo-=o

Using FileType won't work since other plugins set formatoption.

2
  • This seems to be the only solution for me even when I disable all plugins in Vundle. Using neovim. Any idea why this might be? Commented Nov 4, 2019 at 21:52
  • @andrewgazelka It's probably set by an ftplugin that's part of your standard Vim installation (i.e. not something you've added via Vundle). Use :verbose set fo? to see where the option was set.
    – Rich
    Commented Feb 7, 2020 at 12:18
3

My answer is very similar to Martin Tournoij (full credit to him). This just makes it in a single-line command. When you run this, it will disable continuation of comment permanently:

echo 'au FileType * set fo-=c fo-=r fo-=o' >> ~/.vimrc
1
  • Adding line au FileType * set fo-=c fo-=r fo-=o into .vimrc is working. I was planning to create a vim-build of my own without this unnecessary crap. Having this setting saves a lot of time! Commented May 26, 2021 at 13:48

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.