Vim yanking range of lines
Asked Answered
C

10

101

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:

  1. 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?

  2. 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 the g command?

Carloscarlota answered 7/1, 2010 at 19:24 Comment(4)
Do you know about the command ":help" in vim(1)?Declivous
Yes, but I was unable to find it in all the references. It turns out I was thinking I would do it from normal mode, but hometoast showed how it had to be done from command mode. For the g command, I just find that the help doesn't tie everything together as I would expect from the other commands.Carloscarlota
As an aside, I you might want to make one post per question.Teter
I will do so in the future. I was more concerned with the first question, I just thought of the other one as I was typing the first.Carloscarlota
D
162

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.

Debility answered 7/1, 2010 at 19:27 Comment(7)
Wow, that was fast! I didn't try doing it from command mode, duh! Thank you very much for the quick response.Carloscarlota
Using capital letters provides variations, too: gg will go to the first line, while G will go to the last. Also, 5G goes to line 5. Marks can be used in lieu of line numbers also (good for macros), ie: :'a,52yHartsock
If you're not afraid of the Shift key, 81G11Y is even "shorter".Katz
The downside of the normal mode method is you lose your "place"; you could conceivably mx before and 'x after but then the command mode version becomes a lot more succinct. Humm... no sooner had I written this that I saw the answer from @Asta.Androgynous
I have the lines yanked but they don't seem to be copied, is there any additional step to go from yanking to copying?Windburn
@WilliamRoss this actually puts the lines in a buffer for use in vim, not necessarily in your systems Clipboard where you could paste in to, say, Notepad with CTRL+V. See related question: How to copy/paste text from vi to different applicationsDebility
This amuses me because the whole reason I searched for the first solution was to avoid counting lines or doing a subtraction in my head.Coquet
A
28

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.

Alford answered 16/12, 2014 at 9:49 Comment(3)
What does the t. mean here?Mcmahon
From my understanding t can be thought of as till or to and . is the address of the current line.Alford
@Mcmahon it is a synonym for :copyHalliburton
D
16

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.

Danyel answered 4/2, 2015 at 12:11 Comment(9)
I'm wishing I could upvote this more than once. Something that helped me was to use this to yank to a register (in my case, register + i.e., the clipboard): 81G"+y91GOssicle
@Ossicle is there no way to yank directly into a register using the line range syntax? 81,91y and somehow target the register?Mildew
@Mildew Probably, but I don't know. That sounds like a question you could ask on vi.stackexchange.com, if it's not already there or here on SO. I'd be interested. I'm no Vim expert.Ossicle
@Ossicle Had to get creative with the google search but I found it here:#16225866. :81,91y +Mildew
@Mildew :81,91y + does not work. It works for other registers like :81,91y a for example but does not seem to like the + register. Does this definitely work for other people?Ossetia
I've figured it out! :81,91y a works fine but if you want to yank to the + or * registers then you need to prefix it with " e.g. :81,91y "+.Ossetia
@Ossetia definitely works for me (though i use :y *). Your quote may be commenting the register, so not working after all. Depends on how vim was build (with clipboard or not)Halliburton
@Mildew Interestingly space character matters here. For e.g to copy the entire file to a register, used :%y+ which gets yanked into "+. However the same command didn't work for register 'a' in this form: :%ya (gets yanked into default register, and not "a) , but works in this form: :%y a (yanked to "a)Vivl
This is an excellent tip but uses the default register. Although not one shot, it's easy enough to copy after with :let @a=@ and this may work with the clipboard also but I an on Linux. Also y ya yan yank are all the same command, perhaps why space is required for any a-z regCoryza
S
15

I also like to use vim's relative line number option (set rnu) 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.

Sacaton answered 24/5, 2014 at 16:20 Comment(0)
C
5

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.

Coquet answered 11/8, 2019 at 2:31 Comment(6)
:ya is an abbreviation of :yank, so I’m not sure how I feel about yanks all lines. But good job condensing some of the scattered informationHalliburton
I thought so too, until I looked at :help yank. There is no alias ya for yank. You're right though, the a doesn't stand for "all", it technically stands for "a". I prefer to say "all" still though, since it makes more sense to me.Coquet
:[range]y[ank] [x] Yank [range] lines [into register x] the square brackets in y[ank] indicate that the [ank] are optional. :y, :ya, and :yan are implicitly aliases for :yankHalliburton
I stand corrected :) The a is not necessary. Will edit the answer to reflect.Coquet
Your addendum is nice, but I was referring to your very first line, where you use the ex command :ya and not the normal command yHalliburton
Oh, ya is a holdover from ex. From the ex manual: ( . , . )yank buffer count abbr: yaCoquet
A
4

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.

Airs answered 7/1, 2010 at 19:31 Comment(1)
Thank you. I understand now that it's just used for different purposes. When I try to learn these tools, I try to associate the commands with something I can abstract so that I remember how to properly use it. It seems like the g command is just one of those things that you need to memorize to use properly.Carloscarlota
S
3

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.

Stander answered 7/6, 2018 at 12:33 Comment(2)
Disagree, but only bc visual mode is so slow sometimes.Halliburton
This is actually very useful if you want a range of lines copied to a register that you can't get with other motion commands. use "ay or like after selecting.Coryza
J
2

Vim's :help index describes g as:

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

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

Judah answered 7/1, 2010 at 19:37 Comment(1)
I was familiar with the :help but I was NOT familiar with :help (command). That will be very useful in the future. Thank you kindly, sir.Carloscarlota
V
2

As a long time Vi/Vim user I tend to use 'marks' instead of line numbers (or 'line markers'). It works like this: m is the 'mark' character; then use any letter to identify/name the mark. To return to a mark preface the named mark with a single quote ( 'a)These marks can be used as the range. Examples:

File:
    <line 1>
    <line 2>
    <line 3>
    <line 4>
    <line 5>

When in command mode move cursor to line 2, typema. scroll to line 4, typemb. To yank from mark a to mark b type:

    :'a,'byank

To delete from mark a to mark b type:

    :'a,'bdel

To search from mark a to mark b and replace 'ine' with 'ink':

    :'a,'bs/ine/ink/g

To copy mark a through mark b and paste below the current position (the 'dot' always references the line where the cursor currently is positioned):

    :'a,'bco . 

Shift lines of code, between mark a through mark b, one tab to the right (use opposite chevron, <, to move left):

    :'a,'b> 

In command mode you can move back to marks by simply typing 'a to move back to the line marked a. Typing '' moves you back to previous position (unfortuantely only remembers the previous position, not two back).

You can yank to named buffers, copy, delete lines, search&replace just portions of your code, etc. without needing to know the line numbers.

Vanillin answered 25/9, 2019 at 23:54 Comment(1)
vim.fandom.com/wiki/… provides an even more succinct use of markers.Gyration
R
1

To yank lines from line number 81 to 91 :

approach 1: 81gg11yy

not bad but you have to do little bit of math to find out how many lines to yank

approach 2: 81gg then shift+v then 91gg then y

BEST IN MY OPINION because this is straight forward, you only have to know the obvious thing i.e from which line number to which line number you want to yank

Romanticize answered 31/10, 2019 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.