Vim Tips Wiki
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #388 - Insert C++ or LaTeX or other comments easily

Created: December 16, 2002 4:40 Complexity: basic Author: Pavel Version: 6.0 Karma: 214/89 Imported from: Tip#388

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

s/^/%/ 

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.

Comments

Have a look at VimTip194.

SBrueggemann<somewhere at>gmx.net , December 16, 2002 8:03


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.

vim--AT--affenmann.de , December 18, 2002 3:18


Advertisement