I would like to insert a hash at the beginning of a selected block of text in开发者_Python百科 VIM (ruby comment). I selected the lines in Visual Mode, but how do I perform the same operation to all lines?
You have two primary options:
Select in block visual mode (ctrl-v), then use
I
to insert the same thing along the left side of the entire block. SimilarlyA
appends; see blockwise operators.Select the lines in normal visual (v) or visual line (V) mode, then run the same command on all of them, for example
s/^/# /
ornormal I#
. Typing:
while you have a visual selection automatically uses the visual selection as the line range (denoted by'<,'>
).
While in visual mode do the
:'<,'>s/^/#
actually, '<,'>
will be inserted automatically when you hit :
.
You better use this.
COMMAND MODE with set number to see lines
:10,50s/^/#/g
First number before comma is the start line and second number after comma is the end line. Both are included.
Another question might have copied this question, so came here from How to Insert in Visual Block Mode.
Highly recommend that people take a look at this cheat sheet: http://www.rayninfo.co.uk/vimtips.html
As people do more research into VIM people will see a lot of %s/^/# with the % sign in front and by replacing the % sign with what pops up in Visual Block Mode with :'<,'> the symbols that pop up you are able to do insert, etc.
:'<,'>s/^/# (applied on selected lines only)
:%s/^/# (applied globally)
(sharing my two cents after researching how to add a hrefs' to different lines).
精彩评论