对多行添加注释
例如,选中下面代码片的前四行,并通过在每行的行首添加#号,将它们注释掉:
def foo(a,b):
for each in (a,b):
print each
return a+b
print "2"
最终形成这样的效果:
#def foo(a,b):
# for each in (a,b):
# print each
# return a+b
print "2"
方法:
有现成的插件nerdcommenter可以实现对不同语言添加注释的功能。
只用现成的插件感觉不过瘾的话,利用vi内置的操作同样可以实现效果。
Option 1:
** V-blocks
:1 Enter (Go to line 1)
Ctrl-V (V-Block mode)
jjj (Down 3 more lines)
Shift-I (Enter insert mode before the block)
(Insert a ‘#’)
Esc (Back to normal mode)
- Option #2:
-
Substitution
:1,4s/^/#/
Breakdown:
- Ex command follows
1,4 on lines from 1 through 4
s substitute
/ separator for pieces of the substitution command.
(You can also use a different character, e.g. :)
^ beginning of the line
/ separator
the comment character for python
/ final separator
Option #3:
Repeat application of a macro (source)
:1 Enter (Go to line 1)
qa (Start recording on register a)
Shift-I (Enter insert mode at the beginning of the line
(Add a ‘#’ at the beginning of the line)
Esc (Back to normal mode)
q (Stop recording)
:2,4 normal @a (re-run the macro recorded to register a on lines between 2 and 4)
OR
you can select the lines in visual mode and hit : to automatically populate the Ex line with :’<,’> (a range from the beginning to the end of the visual selection) then type normal @a and hit Enter (source).
Now, whenever you want to comment some lines just re-run the macro recorded to register a on those lines:
:9,22 normal @a (comment out lines 9-22)