Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm learning to like vim, but one thing that I haven't yet fully embraced is the copy and paste situation. In a muggle text editor I would Ctrl-C copy a piece of text and then edit/delete and then paste the text somewhere else. However in vim I can't do this because after I copy text with y, modifying the text often involves d and x which obliterates my original text.

Is there any way to make d and x not copy the text? Or otherwise, how should I be using these vim features?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

The 9 previous yanks are saved in registers called 1 through 9. You can recall the next-to-last yank with "1p, the previous one with "2p and so on. The command :reg shows the registers that are available for pasting.

If you want a yank to last longer, use a letter register.

For the more obscure yank-related commands, start reading at :help " in the manual.

share|improve this answer
    
Also, register 0 contains the last text yanked (as opposed to deleted). So referring back to the original question, you can yank with y, continue editing including d and x, and paste the yanked text in with "0p –  Nefrubyr May 16 '12 at 9:15

The commands you're talking about use the so-called "unnamed" register. It is meant only for very short term use. "I'm gonna yank this from here and put it over there" sort of things.

When you want to hold onto something for more than a few seconds while you go and do something else, you should be using named registers.

For instance, " a y y will yank the line the cursor is on into the a register. Nothing will clobber it accidentally, unless you type another command involving the a register. Minutes, hours, even days later, you can type " a p to drop that yanked copy of the line from the a register below the one the cursor is on.

(For this to work really well, you should have a line like set viminfo='50,\"1000 in your ~/.vimrc file, to tell it to remember things like register contents across Vim sessions. You can then go on vacation between yank and put!)

There are 26 named registers (a-z). If you give them as uppercase instead of as shown above, you append to the current register contents instead of replacing them. So, you can build up something really complex in, say, register h one piece at a time, then plop it all down at once with " h p.

Notice that the register name is optional. This implies that there may be many commands you already know and use where you could be using named registers. Say :help registers in Vim to get some idea of the possibilities.

Also, get a Vi mug.

share|improve this answer

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.