2

I am running nVim v 0.10.1 on archlinux. Everything is up to date.

I am running this macro as defined in .vimrc:

let @k =  '\z:call CZC()'

But a carridge return is necesary to make this run, when I key @k so it becomes

let @k =  '\z:call CZC()^M'

This works but ^M executes two carridge returns

  • one run's the call function, and
  • one inserts a carridge return into my currently being edited text as a new line.

I only want one carridge return, that will execute @k not two. How can I add the carridge return at the end of the macro, to run the macro, without adding a new line to my text?

0

2 Answers 2

3

This happens because when you use :let @ to set a register, it detects whether the register should be made linewise based on whether the value ends in a <NL> or <CR>.

See this part of :help :let-@:

If the result of {expr1} ends in a <CR> or <NL>, the register will be linewise, otherwise it will be set to characterwise.

The ultimate side effect of making a register linewise is that a <NL> is appended at the end.

You can see that with the :reg k command:

:reg k
Type Name Content
  l  "k   \z:call CZC()^M^J

The ^J is the added <NL>.

One way around this is using setreg(), in which case you can explicitly set the register to be characterwise even though the contents end with <CR>:

call setreg('k', '\z:call CZC()^M', 'c')

Or, slightly better, using \r in a double quoted string, that way your file doesn't need to have special characters and is less likely to be corrupted:

call setreg('k', "\\z:call CZC()\r", 'c')
3

filbranden has done a great job of explaining what causes the issue.

An alternative to using his suggestion of using setreg() is to end your macro with a Ctrl-J instead of a CTRL-M. You can do this either by typing Ctrl-VCtrl-J to enter it literally*, or with a string escape:

let @k =  "\\z:call CZC()\<C-J>"

You can then see with :reg k that this doesn't cause the :let command to add the additional character:

:reg k
Type Name Content
  l  "k   \z:call CZC()^J

The CTRL-J will still cause your command-line command to be executed, though, as desired.

*: Note that this will be notated as ^@ in the command-line. See :help NL-used-for-Nul.

8
  • It seems that it doesn't work on Windows. Commented Sep 6 at 4:51
  • @VivianDeSmedt Hmm. It's working fine for me in Windows gVim. What version are you using? What exact behaviour do you see?
    – Rich
    Commented Sep 6 at 8:36
  • I'm using gVim 9.1 including patch 1-474. Commented Sep 6 at 8:50
  • @VivianDeSmedt Which bit doesn't work? Does the :let command not set the register correctly? Or is the ex command not run? Or is the behaviour of the macro wrong in some other way? Does using a literal CTRL-J instead of the escaped version behave differently?
    – Rich
    Commented Sep 6 at 12:13
  • the :let works fine. I have the ^J at the end of the register when using the :reg command but when I run the macro @k the ^J doesn't trigger the execution of the command and I have in the command line: :call CZC() waiting an Enter key stroke to be validated. Commented Sep 6 at 12:23

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.