In my Vim configuration, I have mapped the <C-j> key to <cmd>bn<CR>, but because the C-j key corresponds to the null character in Vim, this causes some strange issues when defining macros. For instance, if I define a macro for q with


the macro executes the keystroke <CR>, as expected. But if I try to define @q directly with the keystrokes,

:let @q='^M'<CR>

where ^M is the literal newline character, executing q with @q behaves like <CR><C-j>. And indeed, checking :reg shows that "q=^M^J when defined directly, and "q=^M when defined via macro recording. It seems like this extra null character is only appended when the string ends in a carriage return.

I would like to be able to define "q directly without this terminating null character. My solution for now has been to replace ^M$ with ^M^[. While this works for virtually all cases, it's not perfect (for instance, if the macro ends in the middle of a chord, the <nop> would exit the chord).

Why does assignment and macro recording differ in this way? And is there a way to perform assign a register without this terminating null character?

  • I can reproduce the problem on Windows. Commented Aug 13, 2022 at 21:35

1 Answer 1


I believe the reason is that when you set the register using the let @q= syntax, if it ends with ^M or ^J (<CR> or <NL>) the register is considered line wise (instead of character wise) and and ^J is added at its end.

see :help :let-@

If you want to programmatically set register q to ^M you can do:

setreg('q', '^M', 'c')
  • 1
    setreg works! Thank you!
    – Andy Wang
    Commented Aug 14, 2022 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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