Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a keyboard shortcut in Insert mode to tell vim that I don't want to be inside a comment any more?

When I write // comments in Java, and press Enter, vim helpfully adds a // to the beginning of the next line.

// This is the first line of my comment <CR>
// <-- these were added automatically by auto-comment.

As I say, this is helpful behaviour (I use // for multi-line comments to make it easy to comment out large blocks of code with /*...*/, and use /**...*/ only for Javadoc comments). But when I've reached the end of a comment, I have to press the backspace key three times to get rid of the // at the beginning of the line that I now no longer want.

Is there a keyboard shortcut from Insert mode that can tell Vim that I'm no longer writing a comment? Or do I have to write my own?

like image 320
John Gowers Avatar asked Jul 26 '13 10:07

John Gowers


1 Answers

Since Vim cannot guess your thoughts (yet), you have to tell it explicitly when a new line does not continue a multi-line comment.

A straightforward solution is to simply delete the autoinserted comment leader with CtrlU in insert mode. See :h i_CTRL-U.

This key combo also works in most terminals, by the way.

In case you don't like autoinserted comment leaders at all, you can disable them with the command :set fo-=ro. See :h 'formatoptions'.

like image 96
glts Avatar answered Oct 04 '22 02:10

glts