Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
"if i try to select multiple lines in visual-mode, it isn't written into the register." <-- Really? Works for me... – Billy ONeal Jan 17 '11 at 16:28
and for me too... – ulidtko Jan 17 '11 at 16:32
ah ok, i've tried with "iyv, silly me. should be Vjjj"iy like ulidtko mentioned. – ryz Jan 17 '11 at 17:03
up vote 31 down vote accepted

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.

share|improve this answer
+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
thanks, totally works! – ryz Jan 17 '11 at 17:04
@ryz accept the answer if it was useful – ulidtko Jan 17 '11 at 17:07
gladly accepted it, fastest way for me yet. – ryz Jan 17 '11 at 17:12

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.

share|improve this answer
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
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. – alock27 Apr 17 '13 at 2:21
@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

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

share|improve this answer
Are you sure you don't mean v not m? – Billy ONeal Jan 17 '11 at 16:27

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

share|improve this answer
thanks, works. thank you very much! – ryz Jan 17 '11 at 17:05

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|improve this answer

You must log in to answer this question.

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