1

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?

user515933
  • 11
  • 2

1 Answers1

1

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.

romainl
  • 158,436
  • 18
  • 236
  • 264
  • Thank you very much for your reply! So there isn't a way to interpret this characters using this method, isn't it? Am I obliged to use :let @a = @0 ? – user515933 May 25 '21 at 11:28
  • However I'm a little confused by the manual entry that you recalled. Why is specified "characters that end the command line" if is considered also the character ^M which is inside the text? – user515933 May 25 '21 at 11:50
  • See my latest edit. – romainl May 25 '21 at 12:24
  • Sorry but cannot notice any differences. What did you edited? Your reply or my post? – user515933 May 25 '21 at 12:51
  • Haha… I didn't save my edit. – romainl May 25 '21 at 13:09
  • @user515933, now it's up. – romainl May 25 '21 at 13:21
  • ahaha ok so I'm not totally dumb :) So, I read your edit and that's clarified my doubt about that manual entry. However I have another question (sorry if it wasn't clear by my post): is there a way to convert the character ^M to actual newline character? because when I paste the content of the `"a` register what I see is the exact text `test^Mtest^M` instead of the words test on two lines. Is there a way to actually interpret ^M as a newline character when I paste the content of the register `"a`? – user515933 May 25 '21 at 19:18