created December 16, 2002 · complexity basic · author Pavel · version 6.0
Visual selection combined with powerful replace (:s) command can be used for fast inserting C++ (//), LaTeX (%), and other comments at the beginning of a block of lines.
If you have, for example, paragraph in a LaTeX file and you want to comment it (so that it does not appear in the output anymore), then you have to insert the percent sign '%' at the beginning of every line. An easy way to do this is to select visually the block of text, press ':' for entering a vim command (which automatically expands to :'<,'>) and to use substitute
The whole command then looks like
:'<,'>s/^/<your comment here>/
So just press enter and the comment will be inserted at the beginning of all the selected lines.
If you want to delete it later, just use column blocks (Control-V starts blockwise visual selection) to select first column(s) and d to delete them.
Have a look at VimTip194.
Another way to do the same thing goes as follows:
Mark the area which is to be commented using the blockwise visual mode (CTRL-V, in Windows this is CTRL-Q). Then press I (capital i) and write the text you want to prepend to each line of the selected block, e.g. %. Then press ESC and the text will be inserted to the left of each line of the selected block.
This also works to insert something in the middle of the line, though I mostly use it to add LaTex comments.