2

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()
David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

4

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
Biggybi
  • 266
  • 1
  • 10
  • 1
    Note that (to my great distaste) many ftplugins override this. One solution is an autocommand for Filetype that matches all filetypes and sets formatoptions to what you like. – D. Ben Knoble Jun 19 '20 at 16:36
  • @D.BenKnoble Nice catch, I add an autocmd. – Biggybi Jun 20 '20 at 14:52