0

I'm trying to programmatically record a macro (using :let @m= rather than qm). In vim, I executed:

:let @m = ':retab^M'

where ^M is typed as ctrl+V, Enter. I expect the buffer to contain exactly what is written above, but it also contains a newline at the end, and when the macro is executed, my cursor moves down by one line. If I populate @m with the q record function, this problem does not occur.

How can I programmatically set a register to contain a ^M but not an extra newline? Note that :let @m=":retab\<cr>" gives the same result.

To elaborate: I expect the @m to contain ':retab^M' or ':retab\n' but it actually contains: ':retab\n\n' with an extra newline. Or to say it another way, :let @m = ':retab^M' and qm:retab ENTER q should give the same result (the identical macro in register m), but they differ by a newline character. (The result is the same on Windows and Linux.)

  • Please explain with more detailed example what you try to do what you expect and what you get. I mean, given the text, I try to record a macro that should do 'this and that' instead when I execute it I get 'that and this'. – Maxim Kim Feb 14 at 7:02
1

Could you try to replace c-m to c-j?

like

let @m = ':retab^@'

Not 100% sure why it happens but...

When you do (with <C-v><C-m>)

let @m = ':retab^M'

The m register gets additional 'newline' and looks like:

:retab^M^@

But if instead of ^M you will insert ^@, no additional 'newline' is inserted and you end up with:

:retab^@

PS

to see contents of the register use:

echomsg @m

echo @m will not show those special symbols.

  • This works! Can you tell me what is happening? I can't find <c-j> in the docs. (Note: in full, that is <c-v><c-j>.) – piojo Feb 14 at 7:10
  • I am not 100% sure :) – Maxim Kim Feb 14 at 7:17
  • BTW, :h i_ctrl-j shows no difference with ctrl-m. – Maxim Kim Feb 14 at 7:24
  • Best way to view registers is :reg [letter] – D. Ben Knoble Feb 14 at 14:27
1

You could use the following, if you don't want to <C-V>...:

let @m=":retab\n"

Note: You need double-quotes. Doesn't work with single quotes.

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.