I like /* ... */
(C ansi comments), so here it is my trick for you. You can adapt it to use in different cases, of course.
Comment with /* ... */
Select the text (go to the begin, start visual block, jump with }
):
<c-V>}
Type the command to be applied in the selection
:norm i/* <c-v><esc>$a */
Command will look like: :'<,'>norm i /* ^[$a */
See (i*) for details.
Uncomment the /* ... */
Select the text (as before, or other way you like):
<c-V>}
Type the command to be applied in the selection
:norm :s-\s*/\*\s*-<c-v><enter>$bbld$
Command will look like: :'<,'>norm :s-\s*/\*\s*-^M$bbld$
See (ii*) for details.
Result
Effect is comments line by line:
Comment block
Comment block
Comment block
Becomes (and vice-versa):
/* Comment block */
/* Comment block */
/* Comment block */
Its better to save it as some map
or @reg
in your .vimrc
, because it's a lot to type. If you prefer a single /*
and */
to the whole block, use:
Comment with a single /* */ the whole block
Save it in a register by recording with, say, qc
, then, at the beginning of a paragraph to comment:
v}di/* */<esc>hhhp
and don't forget q
again, to finish the record.
See (iii*) for details.
Uncomment a single /* */ from a block
Save it in register, say, @u
. Put your cursor anywhere inside the block, and:
?/\*<enter>xx/\*/<enter>xx
Save the register by finishing q
command.
See (iv*) for details.
Result
Effect is a single comment for multiple lines:
Comment block
Comment block
Comment block
Becomes (and vice-versa):
/* Comment block
Comment block
Comment block */
Explanations
(i*) It works by using norm
which applies the same command repeatedly in every selected line. The command simply insert a /*
, finds the end of that line and finishes by inserting a */
:norm i/* <c-v><esc>$a */
(ii*) It also uses norm
to repeat the search/replace on every line. Search for spaces /* spaces
and replace by nothing. After that, finds the end of the line, back two words, right a letter, delete to the end.
:norm :s-\s*/\*\s*-<c-v><enter>$bbld$
(iii*) Selects the paragraph by v}
, delete it, insert a comment open and close, move to its middle and paste the deleted block.
v}di/* */<esc>hhhp
(iv*) Anywhere in the middle, finds backwards a /*
, deletes it; finds forward a */
, deletes it.
?/\*<enter>xx/\*/<enter>xx