How can I yank multiple lines into a register?

68,549

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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).

Solution 5

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

Share:
68,549

Related videos on Youtube

ryz
Author by

ryz

Updated on September 17, 2022

Comments

  • ryz
    ryz over 1 year

    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?

  • Billy ONeal
    Billy ONeal over 13 years
    Are you sure you don't mean v not m?
  • Billy ONeal
    Billy ONeal over 13 years
    +1 - but V is Visual Line Mode, v is visual mode.
  • Massey101
    Massey101 over 13 years
    ok, ok, right. Visual Line Mode. %)
  • ryz
    ryz over 13 years
    gladly accepted it, fastest way for me yet.
  • Scott - Слава Україні
    Scott - Слава Україні about 11 years
    The question asked, “how can I yank multiple lines into a register?” Please show how these commands work with registers.
  • awilkening
    awilkening about 11 years
    I hope my edit fulfills your request.
  • Scott - Слава Україні
    Scott - Слава Україні about 11 years
    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 - Слава Україні
    Scott - Слава Україні about 11 years
    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 - Слава Україні
    Scott - Слава Україні about 11 years
    (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 - Слава Україні
    Scott - Слава Україні about 11 years
    (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.
  • awilkening
    awilkening almost 11 years
    @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".
  • AlexMA
    AlexMA almost 10 years
    Was looking for a way to yank the who file into register + for copying to system clipboard. :%y + did the trick, thanks.
  • Julian
    Julian about 7 years
    @BillyONeal 'v' starts Visual Mode, and allows you to do the same thing, 'm' is used for "marking" a region.