10

While inserting a new line below a comment in vim, the result tends to insert a " at the start of the new line. It's probably a simple solution or reason why this is happening, but I am unable to find an exact solution.

Brandon Mercer
  • 403
  • 1
  • 4
  • 10
  • 3
    `` clears the line if you dont want to alter that setting. – romainl Oct 19 '13 at 07:19
  • 1
    See also [Is there a keyboard shortcut in Insert mode to tell vim that I don't want to be inside a comment any more?](http://stackoverflow.com/q/17878768). – glts Oct 19 '13 at 08:14
  • 2
    You may also be interested in [this petition](https://groups.google.com/d/msg/vim_dev/EKDS1PP4rPo/ifLO6FFNqe4J) on the developer's mailing list: some people feel that this setting, `formatoptions=ro`, shouldn't be on by default since it is a user preference. – glts Oct 19 '13 at 08:20

4 Answers4

17

If you’re editing a file of the vim filetype, Vim might by default insert the comment character (in Vimscript, this would be ") at the beginning of each new line you enter after a comment. As already mentioned, this is a result of Vim’s formatoptions setting.

To turn this behavior off in the current file, run

:set formatoptions-=ro

To turn it off by default, add this to your ~/.vimrc:

set formatoptions-=ro

To turn it off for Vimscript files, add this to your ~/.vimrc:

augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal formatoptions-=ro
augroup END

r and o are options which can be given to formatoptions. For the full list of possible options, run :help fo-table.

Ben Klein
  • 1,719
  • 3
  • 18
  • 41
2

This behaviour is governed by the formatoptions variable.

Use :h formatoptions to find out more.

The following article might also be helpful: Disable automatic comment insertion.

Don Reba
  • 13,814
  • 3
  • 48
  • 61
2

I think this should work, regardless of your formatoptions settings.

inoremap <CR> <CR><C-U>

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
1

what command are you using to insert below?

If you use the standard "o" keystroke while in Navigation mode, it should insert a new line immediately below whatever the cursor is on, and automatically place you into Insert mode, without inserting an extra "

Similarly an uppercase "O" will insert a new line above whatever line the cursor is on, and place you in insert mode.

ryan cumley
  • 1,901
  • 14
  • 11
  • I'm well aware of what "o" and "O" does. However, whenever using comments in a file with the .vim extensions, vim creates an addition " at the start of a new line. The " part is what I didn't understand. – Brandon Mercer Oct 21 '13 at 03:19