2

This is more of a specific deeper dive into vanilla Vim then the question Can I make Vim also save “small deletions” into register "1?.

A common text editing situation I run into is to yank a text string and then attempt to replace another string with the one previously yanked.

If I do this on a line Vim generously rotates the registries for me ("1, "2, "…) but with inner text movement you usually get one register and it gets swallowed with the last movement.

So the following example becomes problematic:

foobar = "this is a test"
bazfoo = "this is another test"
                  ^- Cursor

yi"kci"<C-R>-. Now I could preempt this with "ayi"kci"<C-R>a. However, this is slightly problematic because more times than not the later is executed like this instead: yi"kci"crap!<Esc>uj"ayi"kci"<C-R>a.

Obviously this isn't ideal. So I am curious, how do I conveniently manage small change registers or is there a better way to replace a text object with a previously yanked one?

  • 1
    "0p or <C-r>0 use the last yank so you can cut whatever you want in the mean time and keep the ability to put the last yank. – romainl Sep 29 '15 at 14:56
  • hed-palm! Thank you! Can't believe I missed that. Add it as an answer and I'll mark it as accepted. – Sukima Sep 29 '15 at 14:57
6

If you don't use a specific register, the text you yank systematically goes into register 0 where it will stay until you yank something else.

This means that you can still use what you yanked, even if you cut stuff in the mean time.

Paste from register 0 in normal mode: "0p.

Insert content of register 0 in insert mode: <C-r>0.

See :help registers.

  • very cool! (+1) – JJoao Sep 30 '15 at 15:33
0

You can also use visual mode. Your example

foobar = "this is a test"
bazfoo = "this is another test"
                  ^- Cursor

can be changed with yi"kvi"p.

foobar = "this is another test"
bazfoo = "this is another test"

See :h v_p.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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