Vim yanking range of lines

大城市里の小女人 提交于 2019-11-29 20:14:26
hometoast

Yank lines 81-91

:81,91y<enter>

If your fingers don't like to find the : and , keys, this would work as well (go to line 81, yank 11 lines)

81gg11yy 

My only use of g is 5gg. 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.

You can also copy the current lines to your present cursor location using 't'.

:81,91t.<enter>

This will paste the lines 81-91 under the line the cursor is on.

I learned this from http://vimcasts.org which is an excellent resource on VIM.

I also like to use vim's relative line number option which means I can just enter:

:-10,-7ya a

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:

:-10,-7co .

means copy the four lines of text 10 lines above to below the current line, and

:-10,-7mo .

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, like G. So to yank everything until line 91 you can use y91G.

Together you get:

81Gy91G

Go to line 81, then yank while going to line 91.

Jimbo

g doesn't do anything by itself. It's one of a couple meta-commands that holds a bunch of sorta-unrelated commands.

z is yet another command like that.

Vim's :help index describes g as:

|g|             g{char}            extended commands, see |g| below

Scroll down (or :help g) for a list.

The best solution would be to enter "visual mode", by pressing v. And after selecting lines just copy them by pressing y. Then paste copied lines by pressing p.

In addition to :91,96y a which yanks (y) lines 91 through 96 into register a, (pasted with "ap), the yanked lines can be appended to the register with:

:91,96y A

I.e. the capitalization of the A register causes an appending operation into register a instead of an overwrite. Capitalization of the register always works like this, e.g. :let @A=';' appends a ; to register a.

Using plus (+) or minus (-) references lines relative to the current cursor position:

:-10,+10y b

I.e. it would yank(y) 21 lines around the current cursor position and put them in register b.

An absence of input actually represents the current cursor position as well, which means that this:

:-5,y a

would yank the text from 5 lines above to current cursor position into named buffer a, and:

:,+5y a

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:

:+10,+13m.

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 with ya{motion} where the motion was supplied by 91,95. This was incorrect and the "a" in ya is completely unnecessary. In my defense, my help yank does not convey that ya is a possible alias of yank.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!