Do not insert comments when pressing O or [Return] in vim

Viewed 150

Let's say I have the following text in a vim file:

1   "This is a function|
2   function MyFunction()

And my cursor is at the end of line 1 (show by the |). If I press o on that line, it will create a line like this:

1   "This is a function
2   "|
3   function MyFunction()

That is, a line starting with a comment-character. How can I disable this, so that when I press enter/o/O, it just creates a newline at the existing indent, ignoring any comment chars, like this:

1   "This is a function
2   |
3   function MyFunction()
1 Answers

From :h fo-table and :h 'formatoptions':

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

You might want to put something like this in your vimrc:

set formatoptions-=cro

Or an autocmd to avoid ftplugins settings (see comments):

augroup NoAutoComment
  au!
  au FileType * setlocal formatoptions-=cro
augroup end
Related