Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim pasting -- scroll through previously yanked text

Tags:

vim

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?

like image 946
Steven Lu Avatar asked Jun 09 '13 20:06

Steven Lu


People also ask

How do I paste all yanked lines in Vim?

Pasting (Putting) To put the yanked or deleted text, move the cursor to the desired location and press p to put (paste) the text after the cursor or P to put (paste) before the cursor.

How do you paste yanked?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor. To place the yanked line in a new line above the cursor, type P .

How do I paste from clipboard in Vim?

When using Vim under Windows, the clipboard can be accessed with the following: In step 4, press Shift+Delete to cut or Ctrl+Insert to copy. In step 6, press Shift+Insert to paste.

How do I replace a word with yanked in Vim?

Move the cursor to another word (say "third"). Repeat the operation (change word and replace it with "first"). Move the cursor to another word and press . to repeat the change. Yank inner text (text containing cursor which is in quotes).


2 Answers

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".
like image 58
glts Avatar answered Oct 05 '22 03:10

glts


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.

like image 36
hammar Avatar answered Oct 05 '22 01:10

hammar