Difference between @a="<C-r>0" and @a=@0

187 Views Asked by At

I've the following text copied to the "0 register

test
test
  1. If I want to copy the content of the "0 register to the "a register I do

    :let @a=@0
    

    Then, if I paste the content of "a register I obtain

    test
    test
    
  2. Now, to paste the content of the "0 register to the "a register I do

    :let @a="
    

    and then <C-r>0, and the result is this

    :let @a="test^Mtest^M"
    

    Then I paste the content of "a register ("ap) and I obtain

    test^Mtest^M
    

Why are the results not equal?

Specifically, why the newline characters are written literally and not interpreted in the second case?

1

There are 1 best solutions below

5
On

From :help c_ctrl-r:

[…] And characters that end the command line are inserted literally (<Esc>, <CR>, <NL>, <C-C>). […]

--- EDIT ---

<C-r>0 inserts the content of "0 as if you typed it. Since "0 contains a <CR> (two, actually, but the second is irrelevant):

test<CR>
test<CR>

that <CR> would be typed just as you would have typed it, which would result in the command being executed before the typing is finished:

:let @a = "test<CR>

which would throw E114 because of the missing ".

The compromise, here, is to insert a literal ^M instead of typing <CR> to allow the command-line to be finished.

You don't have that problem with :let @a = @0 because there is no typing and no I/O involved: it's just values being passed around.