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?


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?

And see where they were set like so:

:verbose set fo?
      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.


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.