Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim, what is the difference between <c-r>+register and <c-r><c-p>+register with the . command?

Tags:

vim

I struggle to understand the solution to this vimgolf challenge.

The best proposed solution is

cw(<C-R><C-P>")<Esc>w.w.ZZ

Then I tried to do

cw(<C-R>")<Esc>w.w.ZZ

But then the text becomes

(one) (one)
(one)

instead of

(one) (two)
(three)

Can someone help me to understand why . behaves differently with those two commands?

like image 462
autra Avatar asked Dec 16 '13 17:12

autra


1 Answers

This is interesting. The distinguishing characteristic of CTRL-R CTRL-P (insert literally and fix indent) is not used; indent is not involved, and the literal insert also happens with CTRL-R CTRL-R (but that doesn't work, nor are any special chars like <BS> involved here).

Instead of CTRL-R CTRL-P, this also works with CTRL-R CTRL-O; the only commonality (in the help) is that both have:

Does not replace characters!

How this relates to repeat via ., I don't know (you'd have to dig through the source code or ask on the vim_dev mailing list). Basically, it causes the <C-R><C-O> command itself to go into the . register, instead of the literal text which was in the register when you gave the command. This means that when you repeat the operation with ., you'll get whatever's in the specified register at the time of the repeat, instead of just inserting the same literal text again.

This turns Vim from repeating the command as

replace the current word with (, followed by one, followed by )
to
replace the current word with (, insert the register contents of the default register, insert ).

I.e. the first repeat stores the result of the register insertion, whereas the second actually memorizes the action of the register insertion.

You'll see the effect when you list the ". register contents via :reg ., too.

like image 67
Ingo Karkat Avatar answered Jun 01 '23 00:06

Ingo Karkat