I'm a C# developer who has just recently decided to expand my knowledge of the tools available to me. The first tool I've decided to learn is Vi/Vim. Everything has been going well so far, but there are a couple of questions I can't seem to find the answer to:
Lets say I wanted to yank a range of lines. I know there are many ways of doing so, but I would like to do it by line number. I figured it would be similar to how the substitute commands work, something like
81,91y
. Is there a way to do this?I'm a little confused about the
g
command in normal mode. It seems to do a myriad of things and I can't really determine what the g command does at its core. I'm confused on whether or not it's a motion command or a kind of "catch all" for other commands ran through normal mode. Can someone please explain this or point me to a reference that gives a good explanation of theg
command?
Yank lines 81-91
If your fingers don't like to find the
:
and,
keys, this would work as well (go to line 81, yank 11 lines)My only use of
g
is5gg
. To go to the 5th line.22gg
: 22nd line. As jimbo said, it's really only a modifier for some other commands.For completeness, (http://vim.wikia.com/wiki/Power_of_g) explains a lot of how
g
works in command mode.Vim's
:help index
describesg
as:Scroll down (or
:help g
) for a list.To yank lines from line number 81 to 91 :
approach 1:
81gg11yy
approach 2:
81gg
thenshift+v
then91gg
theny
I also like to use vim's relative line number option which means I can just enter:
to yank the text into named buffer a.
N.B. Specifying A will append what you're yanking to the current contents of buffer a.
Don't forget you can also copy blocks of text and move blocks of text around as well with the similar commands:
means copy the four lines of text 10 lines above to below the current line, and
means move the four lines of text 10 lines above to below the current line.
The
G
command goes to a certain line number, if it's accompanied by a count value.81G
puts you on line 81.The
y
command can be combined with a movement, likeG
. So to yank everything until line 91 you can usey91G
.Together you get:
Go to line 81, then yank while going to line 91.
In addition to
:91,96y a
which yanks (y
) lines 91 through 96 into registera
, (pasted with"ap
), the yanked lines can be appended to the register with:I.e. the capitalization of the
A
register causes an appending operation into registera
instead of an overwrite. Capitalization of the register always works like this, e.g.:let @A=';'
appends a;
to registera
.Using plus (+) or minus (-) references lines relative to the current cursor position:
I.e. it would yank(
y
) 21 lines around the current cursor position and put them in registerb
.An absence of input actually represents the current cursor position as well, which means that this:
would yank the text from 5 lines above to current cursor position into named buffer
a
, and:would yank the 5 lines after the current cursor position into buffer
a
.Note: If you have a macro in buffer
a
it was just overwritten by the previous yank, as yank registers and macro registers are really the same thing. Which is why, coincidentally, you can paste a macro, edit it, and then yank it back into it's register. I personally use letters reached by my left hand for yanks, and letters reached by my right hand for macros.Moving blocks of text around, looks like this:
which means move the four lines positioned 10 lines ahead of current cursor, to below the current line.
Addendum
I previously confused
ya
in:91,95ya a
to be somehow synonymous withya{motion}
where the motion was supplied by91,95
. This was incorrect and the "a" inya
is completely unnecessary. In my defense, myhelp yank
does not convey thatya
is a possible alias ofyank
.