3

In Insert mode, I want a mapping which will return to Normal mode, then call a plugin function.

I've tried this:

inoremap <a-1> <ESC><Plug>AirlineSelectTab1

It exits Insert mode, but doesn't then call the plugin.

On the other hand, this works fine:

inoremap <c-l> <ESC><c-w>l

Why can I do one but not the other?

1 Answer 1

3

This happens because the <Plug> command is itself a mapping defined by a plug-in, so you need recursive mappings in this case for it to work.

imap <a-1> <ESC><Plug>AirlineSelectTab1

The usual recommendation is to always use the nore (non-recursive) version of map commands, to prevent interference from mappings created by the user or by other plug-ins, but unfortunately that's not really possible with commands that use <Plug> mappings.

An alternative, to minimize the number of keystrokes where you're considering recursive mappings (and can maybe help with <Plug> commands that are a whole prefix for another <Plug> command, surely a bad practice but you might find them) is to use :normal (not :normal!, which doesn't consider mappings) from your mapping expansion. That also requires using :execute and marking the mapping as <silent> so it doesn't prominently display the Ex command line during execution.

inoremap <silent> <a-1> <ESC>:execute "normal \<Plug>AirlineSelectTab1"<CR>

That's probably not worth it for a simple case like this one, but might be relevant for more complex mappings that involve many keystrokes other than the call to the <Plug> command itself.

4
  • The last inoremap doesn't seem to catch the <Plug> mapping for me, but inoremap <c-l> <ESC>:normal <c-w>l does. (after setting nmap <c-w>l <Plug>AirlineSelectTab1)
    – perelo
    Nov 22, 2019 at 14:58
  • First solution works like a dream. The second with :normal didn't work for me.
    – LondonRob
    Nov 22, 2019 at 15:04
  • @perelo Fixed now, using :execute, thanks for catching the issue!
    – filbranden
    Nov 22, 2019 at 15:32
  • 1
    Thanks, I learned something! Worthy to note that double quotes " are required instead of single ' so that \<Plug>... gets interpreted as the mapping :h expr-quote :h expr-'. Anyway, first solution may violate the "only use nore mappings" adage but it is the way to go with <Plug> stuffs.
    – perelo
    Nov 22, 2019 at 17:26

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.