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.

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'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
up vote 14 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
    
@Nefrubyr, your comment above is actually the simplest answer, but it's not mentioned in any of the existing three answers. Perhaps you should add it? (I may if you don't.) – Wildcard Apr 29 at 20:34

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

If you need to delete text with d without altering your yank register (say, you've already yanked text in it that you're not ready to paste before your delete some other text for instance), you can use the black hole register _.

If you do "_dd for example, the current line will be immediately deleted and forgotten.

Actually, I don't use this register very often. It doesn't take less keystrokes than saving to a general-purpose register (any letter register) and if you save to a general-purpose register, at least, the lines that you have deleted are still available until you overwrite the register, if the need arises.

If you tend to forget to use another register when deleting, you can define a wrapper macro for dd for instance like this: :noremap dd "add.

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.