21

I am trying to improve the usability of the paste functionality in Vim because too many different deletion operations (in fact I do reckon it's all of them) will also yank to the paste buffer.

What this means is that I am no longer able to delete some piece of text I want to paste somewhere, clean something up, and then do my paste. I don't know why this is the order that I prefer to do things, but I'm not about to change it.

I have to basically do the move "atomically" before returning to do clean-up, else I get a frustrating paste of a comma or bracket or space. Oh, I know the reason why I do it in the other order. It's just plain more efficient. I wouldn't have to move to the destination, then return to clean up, then go back again.

How to improve this? My suggestion is a plugin that can be used to augment the paste operation after the fact. hit p, see that it pasted a useless ephemeral deleted char, and at this point (immediately after a paste operation) our plugin will allow a key to cycle through the previously delete-yanked registers, updating our paste in-place.

This way I can delete things all I want, and I'll actually be able to pull up any recent deleted item quickly, so long as it was a contiguous segment. Which is of course easy to set up with a visual selection followed by a delete. This trades specificity for ease-of-use, as I no longer need to remember to specify some specific named register to use for a particular paste.

In particular, there should be a stack that both yanks and deletes accumulate into, that is later quickly traversed during pasting using a single bind.

Is there a plugin out there that already does this?

4
  • I read this 3 times now and I don't get the question. Are you looking for on how to use registers to store different text to be pasted? Jun 9, 2013 at 20:18
  • Have you used a plugin like YouCompleteMe? I'm talking about a plugin to retroactively update a "wrong paste" into the earlier one in the yank buffer in place. I.e. I hit p, "0 is inserted. I wanna hit plugin key to replace the just-pasted "0 with "1, etc.
    – Steven Lu
    Jun 9, 2013 at 20:18
  • Nope, that one was new to me. Tend to keep my vim as clean as possible... Jun 9, 2013 at 20:20
  • Sure. YCM is definitely one of the more difficult plugins to get going as it has a native compiled component and requires python... but it's all worth it to me
    – Steven Lu
    Jun 9, 2013 at 20:30

3 Answers 3

36

You are a bit confused about the numbered registers (:h quote_number).

There is only one yank register and that is "0.

Separate from that there are nine numbered delete registers "1 to "9. These are filled as a queue with the most recent delete at the top.

For the delete registers "1 to "9 Vim has the functionality you are asking for built in: You can paste "1p, and if it isn't what you're looking for you can repeat u. u. u. to toggle through the registers "2, "3, "4, etc., until you've found the right one. This behaviour is documented at :h redo-register.

Scrolling through previously yanked text is most often done with plugins, the most popular ones I know of being

  • YankRing.vim, "Maintains a history of previous yanks, changes and deletes", and
  • yankstack, "lightweight implementation of emacs' kill ring for vim".
1
  • 1
    Hey thanks for clarifying this behavior. It's pretty hard to get right without someone explaining it like this (even with, took me several attempts). It looks like it requires you to specify "1p before the dot will function by continuing up the registers, and like you mention the u must be used. Being default functionality, though, this is not too terrible, though it is not practical for my use because having to type "1p after p fails (which means I gotta hit u too) feels way too cumbersome, if powerful. the yankstack plugin is exactly what the doctor ordered. Thanks!
    – Steven Lu
    Jun 10, 2013 at 1:02
8

Why not use a named register? For example, use "ay to yank to register a, then "ap to paste from it again later, and it won't get clobbered by normal deletes etc. in the meantime.

1
  • 2
    Right. I addressed this in the question. Consider this is at minimum six additional keystrokes, shift+'+a twice. All different keys. In a specific order. This will never be fast. The yanks are already entered into the number-named registers. Just need a way to cycle through them.
    – Steven Lu
    Jun 9, 2013 at 20:22
5
[l]orem ipsum dolor sit amet

    yiw yanks "lorem" in @" and @0

lorem [i]psum dolor sit amet

    daw deletes "ipsum " into @", @0 still contains "lorem"

lorem dolor [s]it amet

    ciwfoo deletes "sit" into @" and inserts "foo", @0 still contains "lorem"

Because register 0 still contains what you yanked (lorem), even after a lot of c, d, s, doing "0p will always paste lorem until you explicitly yank something else.

My example only involves what Vim calls "small delete" so the numbered registers are not used; when you do larger operations Vim also uses registers 1-9 but it doesn't change the behavior of register 0 which always keeps the latest explicit yank.

:h registers is a good and easy to understand read: Vim has a lot of features and plugins are not the answer to everything.

Also, see this answer of mine suggesting the black hole register.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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