3

When using <cmd>...<cr>, there are some special keys such as <plug> or <cmd> that are not allowed to be contained inside the block. Trying to execute the command will result in:

E5521: <Cmd> mapping must end with <CR> before second <Cmd>
E5522: <Cmd> mapping must not include <Plug> key

However, sometimes these special keys may appear in the mapping, not to be executed, but inside strings. A practical example is vim-repeat. If you want to set it up by calling repeat#set, there'll most likely be a <plug> key in the string passed to the function:

<cmd>call repeat#set("\<Plug>MyWonderfulMap", v:count)<cr>

The "\<Plug>" should not part of the execution, but simply part of the string. However, the above will still fail, since <Plug> is still between <cmd> and <cr>. We are forced to use the : alternative:

:call repeat#set("\<Plug>MyWonderfulMap", v:count)<cr>

This is sub-optimal however, since it does not work for insert and visual modes.

So knowing that, is there a way to pass the key <plug> inside a function, without it being caught by the <cmd>...<cr> block?

1
  • One option that doesn't involve tricky escapes is to define a new function (and use <Plug> etc. in its body) and then call that function from the <Cmd>.
    – filbranden
    Aug 17, 2021 at 3:38

1 Answer 1

4

It is possible if you manage escaping rules very carefully. For example,

nnoremap x <cmd>echo "<bslash><lt>Plug>MyWonderfulMap" v:count<cr>

Then :map x prints:

n  x           * <Cmd>echo "\<Plug>MyWonderfulMap" v:count<CR>

And typing 3x echos, as expected,

<80><fd>SMyWonderfulMap 3

Breaking what is happening here down by interpolation step:

  1. nnoremap substitutes all the <> escape codes, <bslash> becomes \ and <lt> becomes <.

  2. when the map is executed, normal double quotes rules substitutes \<Plug> with the sequence <80><fd>S which is vim's way of denoting plug maps.

Your Answer

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

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