Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Vim from automatically inserting the comment leader when Enter is pressed?

Tags:

vim

If I'm typing a comment in gVim like this

// this is a comment

and I hit ENTER, it will automatically start the next line with //, so it looks like this:

// this is a comment
//

But usually I don't want to write more comments when using this commenting style. Can I stop gVim from automatically doing this, while still keeping the auto-completing of the /* .. */ commenting style?

like image 677
Merijn Avatar asked Feb 04 '11 08:02

Merijn


People also ask

How do I turn off auto comment in Vim?

To permanently disable this behavior, add autocmd FileType * set formatoptions-=cro in your . vimrc / init. vim .

How do I comment multiple lines in vim?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.


1 Answers

To disable it while hitting ENTER in insert mode, do :set formatoptions-=r

To disable it while hitting o or O in normal mode, do :set formatoptions-=o

See :help 'formatoptions' and :help fo-table.

Alternatively, you can still press CTRL-U in insert mode if you want to delete characters from start of line till the cursor.

like image 149
Benoit Avatar answered Sep 24 '22 13:09

Benoit