1

Consider the following mappings:

nmap <plug>test a123
nmap M <plug>test<plug>test

If both <plug>test were expanded, vim would type out in the buffer "123a123". However, only the first <plug>test is expanded, and vim types "123<Plug>test" instead.

Is there a way to exapand all <plug> mappings before they are executed?

1

I found a way around it. Vim actually tries to expand the <plug> mapping in insert mode, however it can't, since the mapping was set in normal mode only, with nmap.

You may expand it in insert mode by defining the map in that mode as well:

nmap <plug>test a123
imap <plug>test a123
nmap M <plug>test<plug>test

Not exactly "expanding <plug> before execution", but it works around the problem.

3
  • No feedback about the points I raised in my answer?
    – B Layer
    Aug 20 at 22:53
  • @BLayer I understand where you come from, but I disagree that it's a better solution. The intention of the question is to have a mapping that works into insert mode (i.e. it works the same way even after entering insert mode). If the intention of the user is having a mapping that works the same way on both modes, then just mapping to both is the simplest solution. Aug 21 at 5:06
  • I wasn't trying to convince you that my answer was better it was about, for one, the weird dual purposing of "a". Never mind.
    – B Layer
    Aug 21 at 8:29
1

Let's look at the big picture here. nmap <plug>test a123 defines a Normal mode mapping that enters Insert mode, appends "123" and then remains in Insert mode. (I'm assuming that that's a requirement or I'd suggest something a little different.) So a result of "123<Plug>test" makes sense.

Rather than look for some "trickery" to get around the Vim's rules I'd suggest that defining the nmap M command to do exactly what you want to do is the best course of action. By which I mean...

nmap M <plug>test<esc><plug>test

Here's the problem, though. That prints "123123" but it sounds like you actually want to print "123a123". That's, um, kind of weird since you want that "a" to be treated as Normal mode append in the first case and the literal character "a" in the second case. For lack of a better word, that makes things a bit "hacky" and really prevents finding a solution that isn't itself a bit hacky.

Anyways, if that's what you want and not just an artifact of the example code you chose then a slight modification to the above mapping would be necessary:

nmap M <plug>testa<esc><plug>test
2
  • FYI, this is as much about exposing the peculiar nature of the request (i.e. the dual-purposing of that "a") in order to make sure OP's example exactly matches their real world problem as it is to suggest an alternative solution.
    – B Layer
    Aug 19 at 3:09
  • 1
    AFAIK the last line is also why you see a lot of <plug>(test) with brackets, to prevent things running together/being ambiguous/etc.
    – D. Ben Knoble
    Aug 19 at 10:48

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.