Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers

up vote 22 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
add comment

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
1  
@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
show 5 more comments

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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