How do I stop Vim from automatically continuing comment markers when I press Enter or open a new line?
Answer
:set formatoptions-=cro
Explanation
By default, Vim continues the current comment leader when you press <Enter> in insert mode or open a new line with o/O. This is controlled by the formatoptions setting and its flags. Removing the c, r, and o flags disables this automatic comment continuation.
How it works
The relevant formatoptions flags:
c— auto-wrap comments attextwidthand insert the comment leaderr— automatically insert the comment leader when pressing<Enter>in insert modeo— automatically insert the comment leader when pressingoorOin normal mode
Removing all three:
:set formatoptions-=cro
To make this permanent for all filetypes, add it to your vimrc. But since ftplugins often re-enable these flags, use an autocmd to ensure it applies after filetype detection:
autocmd FileType * setlocal formatoptions-=cro
Example
With the default formatoptions (includes cro), typing <Enter> after a // comment line in a C file produces:
// first line
// ← cursor here (unwanted)
After set formatoptions-=cro, pressing <Enter> gives a clean empty line:
// first line
← cursor here (clean)
Tips
- Check your current
formatoptionswith:set formatoptions? - Use
+=to add flags and-=to remove them without affecting other flags - The
nflag (formatoptions+=n) makes Vim recognize numbered lists when reflowing withgq - The
jflag removes a comment leader when joining lines withJ