Google
 

9/22/10

vi editor: multiline comment

1. :x,y s/^/# /

The colon is command mode, obviously
x and y is a range of line numbers you want to affect
s is a sed substitution, the caret (^) means the beginning of the line
The sharp (#) is the comment character, again obviously
I put a space after the comment, but it isn't necessary, substitute any string that suits you.



2. Another way to do do this is to visually select the lines you want to comment with shift-v and up and down arrows, then type

:s/^/# /

Easier for blocks of fewer lines.

NB: When you press : vim will add '<,'> to the command line so the final command is:

:'<,'>s/^/# /

9/20/10

Vim: comment multiple lines


Vim Tip: Comment out multiple lines

Commenting out a bunch of lines without a vim plugin:
Select your lines with VISUAL BLOCK (CTRL-V), then press I to insert before all highlighted lines. Next type your comment character, # (for python, shell, etc). Last press ESC.
I forget not frequently used, but helpful VIM commands from time to time. Why not blog it?
You can alternatively select your lines with VISUAL LINE (SHIFT-V), then type : s/^/#
This tells the selected lines that you wish to substitute the start of the line with the # char.

vim cut past; copy and past

Ever try to cut (or copy) some lines and paste to another place? If you need to count the lines first, then try these to eliminate counting task.


Cut and Paste:
1. Place the cursor at the beginning of the block you want to CUT.
2. Mark it with md
3. Go to the end of the block.
4. Cut it with d'd
5. Go to the new location that you want to PASTE those text.
Press P.

Copy and Paste:
1. Place the cursor at the beginning of the block you want to COPY.
2. Mark it with my
3. Go to the end of the block.
4. Copy it with y'y
5. Go to the new location that you want to PASTE those text.
Press P.

The name of the mark used is related to the operation (d:delete or y:yank).
I found that those mark names requires minimal movement of my finger.


Visual line mode (by shift+v):
delete by: d
Visual block mode (by ctrl + v):
delete by: c