Brandon Mercer
Brandon Mercer

Reputation: 433

inserting a new line below a comment in vim?

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.

Upvotes: 11

Views: 3866

Answers (4)

Christian Brabandt
Christian Brabandt

Reputation: 8248

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

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

Upvotes: 2

Ben Klein
Ben Klein

Reputation: 1949

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.

Upvotes: 18

Don Reba
Don Reba

Reputation: 14031

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.

Upvotes: 2

ryan cumley
ryan cumley

Reputation: 1931

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.

Upvotes: 2

Related Questions