History
Article Edit this page Discussion

Insert C++ or LaTeX or other comments easily

From Vim Tips Wiki

Jump to: navigation, search

[edit] Duplicate tip

This tip is very similar to the following:

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

Tip 388 Previous Next 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

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.

[edit] Comments

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.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Dark Shadows
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for collaboration of the month.

Send this article to a friend
"Insert C++ or LaTeX or other comments easily"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation