How can I yank multiple lines into a register?

45

20

I want to yank multiple lines in a single register in vim, to easily paste different text templates in a document.

For example, "iyy yanks only the the current line, if I try to select multiple lines in visual-mode, it isn't written into the register.

Any suggestions?

ryz

Posted 2011-01-17T15:51:26.253

Reputation: 595

Similar: How to store all occurrences of a multiline search in a register?

– kenorb – 2017-09-15T19:22:29.337

"if i try to select multiple lines in visual-mode, it isn't written into the register." <-- Really? Works for me... – Billy ONeal – 2011-01-17T16:28:58.043

and for me too... – ulidtko – 2011-01-17T16:32:40.333

ah ok, i've tried with "iyv, silly me. should be Vjjj"iy like ulidtko mentioned. – ryz – 2011-01-17T17:03:44.977

Answers

63

Use 3Y to yank 3 lines into the default register; "i3Y for yanking into register i.

Also, my favorite way is not to count the lines I want to yank, but to select them in visual mode via V and moving commands, and then hit y to yank it or "_y to yank into a register.

Also, I have just tried selecting multiple lines in Visual Line mode and yanking into not-default register, e.g. Vjjj"oy — and it works.

ulidtko

Posted 2011-01-17T15:51:26.253

Reputation: 2 356

1+1 - but V is Visual Line Mode, v is visual mode. – Billy ONeal – 2011-01-17T16:27:58.323

ok, ok, right. Visual Line Mode. %) – ulidtko – 2011-01-17T16:31:35.933

1gladly accepted it, fastest way for me yet. – ryz – 2011-01-17T17:12:03.483

25

From anywhere within the file, you can use the following.

:2,5y a

Yank lines 2 - 5. INTO REGISTER a

:7pu a

Paste register a under line 7.

awilkening

Posted 2011-01-17T15:51:26.253

Reputation: 359

1The question asked, “how can I yank multiple lines *into a register*?”  Please show how these commands work with registers. – Scott – 2013-04-10T19:14:28.323

I hope my edit fulfills your request. – awilkening – 2013-04-12T16:34:56.330

Sorry, no.  The question explicitly mentioned "register" repeatedly, referred to "easily paste different text templates", and gave an example using a non-default register. – Scott – 2013-04-12T17:09:07.013

My apologies. Your first comment was unclear as to what a valid and correct answer should contain. I falsely presumed that the asker would understand, like myself, that many vim commands work in multiple modes. Since the asker showed his aptitude in yanking into both default and named registers, I foolishly omitted that component of my answer. I would also note that this works best with :set number so one may see the line numbers of the range to yank. Now, if you'll please remove your down vote as my answer is clearly the best. – awilkening – 2013-04-17T02:21:43.513

For your information, when you respond to a comment (in a new comment), it’s conventional to mention the author’s name, preceded by “@”, as in “@Scott” (don’t include spaces in the name).  That way he gets notified.  See the Replying in comments paragraphs of the Comment formatting section of the Markdown Editing Help page.  I haven’t been doing that here because the author of a post is automatically notified of comments to that post (as, I presume, you have experienced).

– Scott – 2013-04-19T21:49:50.403

You say, “I falsely presumed that the asker would understand, like myself, that many vim commands work in multiple modes.  Since the asker showed his aptitude in yanking into both default and named registers, I foolishly omitted that component of my answer.”  First of all, it may be more accurate to say that vim has multiple modes, and different commands are available in the different modes.  (continued...) – Scott – 2013-04-19T21:50:59.923

(continued)  Secondly, I believe that your statement makes about as much sense as “I recommend Alice’s restaurant.  I presume you know how to drive; therefore, I *won’t* give you directions how to get there.”  The point is that different modes are … what’s the word I’m looking for here? … oh yes: different modes are *different*.  The best example is the “mark” command, which is m in visual mode and k in command mode.  That’s why I asked you to show how the :y and :pu commands work with registers.  (continued...) – Scott – 2013-04-19T21:52:13.863

(continued)  Thirdly, you’re not sending an email to “ryz” (the person who asked the question).  You’re posting an answer that may very well sit here for years and be found by Google searches by people who don’t know vim as well as “ryz” does, so you should provide useful information.  But I’m leaving out the biggest thing: *YOUR ANSWER IS WRONG!*  The right answer is :y a and :pu a.  Finally, you say, “my answer is clearly the best.”  Why in the world do you believe that?  Even after you correct the error with the " characters, your answer will be only as good as the others. – Scott – 2013-04-19T21:56:35.727

3@Scott My apologies. Why didn't you just say, "Dude, check your method with :reg. You'll see it doesn't work." And I would've been all like, "Whoa, my bad". – awilkening – 2013-06-04T18:00:51.013

1Was looking for a way to yank the who file into register + for copying to system clipboard. :%y + did the trick, thanks. – AlexMA – 2014-06-06T14:17:11.517

10

Use m to mark the start, with a buffer name (so you might type mx). Move your cursor down to where you want to stop copying, and type y'x (or d'x if you're cutting and pasting). Then move the to the point where you want to paste, and type p.

The Vim command cheat sheet

Nifle

Posted 2011-01-17T15:51:26.253

Reputation: 31 337

@BillyONeal 'v' starts Visual Mode, and allows you to do the same thing, 'm' is used for "marking" a region. – Nifle – 2017-05-02T07:01:45.750

Are you sure you don't mean v not m? – Billy ONeal – 2011-01-17T16:27:24.673

6

You prefix the command with a number to get how many lines to operate on. You could also use a 'text-object' (like ']' for block, and ')' for paragraph) - this would work on multiple lines contextually - the default is often a single line. For example, "r5yy would yank five lines starting at the cursor into the 'r' register. (Or you could type :.,+4y r to do the same in ex mode.) You can combine numbers with text-objects as well; "r10y).

Arcege

Posted 2011-01-17T15:51:26.253

Reputation: 1 883

1

It's possible to yank multiple lines in case when it is a last search occurrence.

For example given the following a multiline non-greedy pattern:

/start\_.\{-}end/norm gn"iy

then you'll have your yanked multiline pattern (between start and end) in your @i register (print by echo @i).

Related: How to print a multi-line match? at Vi

kenorb

Posted 2011-01-17T15:51:26.253

Reputation: 16 795