vimtricks.wiki Concise Vim tricks, one at a time.

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 at textwidth and insert the comment leader
  • r — automatically insert the comment leader when pressing <Enter> in insert mode
  • o — automatically insert the comment leader when pressing o or O in 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 formatoptions with :set formatoptions?
  • Use += to add flags and -= to remove them without affecting other flags
  • The n flag (formatoptions+=n) makes Vim recognize numbered lists when reflowing with gq
  • The j flag removes a comment leader when joining lines with J

Next

How do I paste a blockwise register without padding lines with trailing spaces in Neovim?