Say I have a bunch of lines:
@Override
public void draw(Graphics g) {
g.setColor(Color.MAGENTA);
g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
g.setColor(Color.BLACK);
g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
When I want to comment them out with //
(i prefer line comments instead of block comments), what I do is:
- Place the cursor infront of the
@
symbol Ctrl-V
: Switch to enter block-select mode- Select the column down to the
}
closing parenthesis using multiple hits ofj
Shift-I
: to enter block-insert- Type
//
ESC
to excitEnter
to finish the command
--> The lines are now commented out.
Is there an easier way where I don't need to do the block-select? I found I can use a substitution like :'<, '>s/^/\/\///g
but this has two problems:
- Its very clumsy and error prone to type (multiple forward and backward slashes need to be escaped)
- It places the comment symbols (
//
) at the beginning of the line (position 0), not at the position where the first character of that line was (so indentation is lost).
How can I insert //
on the selected lines at the position of the first character of each line using Vi?