50

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?

66

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.

  • 1
    Confirmed. Also, it's answered here: stackoverflow.com/questions/16030639/vim-formatoptions-or – Melon Feb 19 '15 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. – Ixx Mar 30 '18 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 Jul 10 at 11:01
1

Add this in your vimrc:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.