sponsor Vim development Vim logo Vim Book Ad

basic Tip #832: Remembering previous deletes/yanks

 tip karma   Rating 62/26, Viewed by 1666 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   December 10, 2004 13:12      complexity:   basic
author:   Kevin Lyda, added by Rory McCann      as of Vim:   5.7

The following was posted on the Irish Linux User Group mailing list:

<quote>
ok, let's say your cursor is on ok, and you delete this paragraph with
the command [d}] (just the d} part, for this mail i'm wrapping
keystrokes in []'s).  the reason you deleted it is because you want to
move it to another file.

so now you open the other file with [:e other.file.txt<return>].  as
you're scrolling down to where you want to put it, you accidentally
delete/yank some text.

you've saved the last file and now doing [p] just inserts the text you
just deleted/yanked.  how can you get back what you deleted?  vim
extends vi's numbered delete buffers so that they work from one file to
the next - switching files doesn't delete the numbered delete buffers.

the numbered buffers are numbered from 0 to 9.  ["0p] is the same as
plain old [p].  ["1p] is the buffer from the previous delete, ["2p] is
from the delete before that, etc.  note that while ["0p] has the last
delete or yank, the other numbered buffers only contain previous
deletes.

it's handy, and thanks to donncha for looking it up the other day while
i was on the train.

</quote>
Credit where credit due, it was Kevin Lyda who posted this.

 rate this tip  Life Changing Helpful Unfulfilling 

<< regexp to strip redundant zeroes in decimal fractions | Google lookup as a contextual spell check and phrase verifier. >>

Additional Notes

Anonymous, December 11, 2004 9:21
type :reg
zzapper, December 11, 2004 15:31
This is pretty magic as well

"1p....

put each numeric register successively

(where the dot is the vi(m) repeat operator)
[email protected], December 12, 2004 5:03
Isn't it pity that this works only for yanks longer than one line ?
Perci Merci., December 14, 2004 16:25
This is called yank-pop in emacs, it is very useful.
Emacs has unlimited depth for yank-pop, and it will
cycle thru the list when pasting.

However, in Vim it is limited to 10 registers and
not intuitive.
sef@medi_notes.com, December 29, 2004 12:09
["0] does not give you the last delete, it gives you the last yank. The last delete: ["1]

I may have something set different although I don't think so.
[email protected], January 10, 2005 7:25
After doing a "1p you can also cycle through the numbered registers by doing u. repeatedly (i.e. remove the last paste and paste the next numbered register, much like emacs' M-y).
RichardBronosky, May 27, 2005 11:36
Multiple deletes get cycled through the 1-9 registers, but multiple yanks do not.
eg: If you want to do 3 different deletes and then paste them somewhere else, this tip works for you.  If you want to do 3 different yanks and paste them somewhere else, you are out of luck.  You manually have to do a "1, "2, "3 before each.

Is there a way to get yanks to cycle registers like deletes?
RichardBronosky, August 29, 2005 8:56
Oh, and here is the kicker!

This really hurts me so much that I sometimes launch TextPad to avoid this behavior...
1. Use "v" to select some text that you want to paste in a bunch of places.
2. Use "y" to yank it.
3. Use "v" to select some text that you want to replace.
4. Use "p" to paste.
5. Use "v" to select the next chunk of text that you want to replace.
6. Use "p" to paste. OH BUT WAIT THAT'S NOT THE TEXT I WANTED TO PASTE!  It pastes the text from step 3, not step 1.

I really wish yanks cycled the registers like deletes do.  Again I ask, "Does anyone have a solution?"
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo