55

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?

4

7 Answers 7

76

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.

3
  • 1
    +1 - but V is Visual Line Mode, v is visual mode. Jan 17, 2011 at 16:27
  • ok, ok, right. Visual Line Mode. %)
    – ulidtko
    Jan 17, 2011 at 16:31
  • 1
    gladly accepted it, fastest way for me yet.
    – ryz
    Jan 17, 2011 at 17:12
32

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.

10
  • 1
    The question asked, “how can I yank multiple lines into a register?” Please show how these commands work with registers. Apr 10, 2013 at 19:14
  • I hope my edit fulfills your request.
    – awilkening
    Apr 12, 2013 at 16:34
  • Sorry, no. The question explicitly mentioned "register" repeatedly, referred to "easily paste different text templates", and gave an example using a non-default register. Apr 12, 2013 at 17:09
  • 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
    Jun 4, 2013 at 18:00
  • 1
    Was looking for a way to yank the who file into register + for copying to system clipboard. :%y + did the trick, thanks.
    – AlexMA
    Jun 6, 2014 at 14:17
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

2
  • Are you sure you don't mean v not m? Jan 17, 2011 at 16:27
  • @BillyONeal 'v' starts Visual Mode, and allows you to do the same thing, 'm' is used for "marking" a region.
    – Nifle
    May 2, 2017 at 7:01
7

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

0
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

0

Somehow we’ve missed that yank is an operator, which means it works on all motions and text-objects (see the relevant help sections).

That means that yip, y), ygg, yib, etc., all work (and with registers, too)! They can potentially be multiple lines.

Note that some motions are not line-wise by default. You can use yV{motion} to force a line-wise yank.

0

Might be not much related to this question. However, if someone needs to copy all lines into a register do it with %

:%y a

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .