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:
@
symbolCtrl-V
: Switch to enter block-select mode}
closing parenthesis using multiple hits of j
Shift-I
: to enter block-insert//
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:
//
) 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?
You can define a custom mapping or command for your :substitute
.
However, there are several commenter plugins that do this very well, and those are generic (and often extensible) so that they work for any filetype:
I'd highly recommend to use one of those plugins instead of trying to reinvent a poor solution yourself.
I use Commentary as in the other answer, but a few thoughts:
<C-v>jjjjj
could be <C-v>}
or <C-v>/}<CR>
:substitute
doesn’t have to use /
as a separator: :'<,'>s-^-//
:'<,'>normal! I//
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With