44

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?

59

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.

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

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.

  • 1
    The question asked, “how can I yank multiple lines into a register?” Please show how these commands work with registers. – Scott Apr 10 '13 at 19:14
  • I hope my edit fulfills your request. – alock27 Apr 12 '13 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. – Scott Apr 12 '13 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". – alock27 Jun 4 '13 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 '14 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

  • Are you sure you don't mean v not m? – Billy ONeal Jan 17 '11 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 '17 at 7:01
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).

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.