I'm trying to map a function to <CR> but the problem is, that another plugin already mapped something to it. So that's the code from me:

function Test()
    echom "Test succeeded!"

inoremap <CR> <CR><CMD>call Test()<CR>

Now if I do :verbose imap <CR>, than I'm getting this: output of ":verbose imap "

If I map it manually again in a vim session with this command:

:inoremap <CR> <CR><CMD>call Test()<CR>

, than I'm getting my desired output Test succeeded!.

Is there another solution than swapping creating an issue for the lexima-plugin or removing it?

I think that this situation needs imap instead of inoremap, is that right?


Ok, so I've tested it with two self-made functions:

function Test()
    echom "Test succeeded!"

function LMAO()
    echom "LMAO SUCCESS!"

imapping both functions manually to <CR> doesn't work either. I can only map one function to <CR>.

1 Answer 1


I'm not sure what you want to achieve.

If you just want to override lexima mapping, then do (you'll have to declare your plugin after)

If you want to have both, well, if I'm not mistaken, it really makes sense for lexima to map <CR> and not something else -- as it's a bracketing plugin. IMO it's not up to lexima to use another keybinding.

I see three possibilities in that case

  • move lexima mapping to another keybinding
  • execute both mappings, one after the other
  • chose one or the other depending on the context -- for instance, after a {, lexima mapping is the one that should be applied.

In all cases, you'll have to extract the current mapping on CR with maparg('<cr>', 'i', 0, 1) and then... things become a little bit more complex as expression mappings shall not be handled as non-expression mappings, may be there was no mapping before, and so on.

In lh-brackets (another bracketing plugin), I have a support function named lh#brackets#enrich_imap() that helps enrich any existing insert-mode mapping (may be there was none), with something another behaviour if a given context is detected -- for instance lh-brackets enriches <cr> mapping with one of its own only when the cursor is detected in between a pair of curly brackets.

  • Currently I'm writing a pull-request for the cosco plugin (an auto-comma-and-semicolon setter). In my pull request, vim should set it automatically after hitting the <CR> key. The easiest solution would be to map the main function to <CR> but it's "blocked" due to lexima.
    – TornaxO7
    Feb 18, 2021 at 21:17
  • What behaviour would make sense in {|} case (cursor in between the brackets) in your eyes? IMO, something like my enrich_imap() function to prioritize lexima/lh-brackets/coc-pair/vim-pair/... mapping on <cr> when the cursor is in between the curly brackets, but defaults to your plugin instead in other situations seems to be a good solution. Feb 18, 2021 at 21:32
  • If I understand your enrich_imap function, than I'd have to write explicit the other function's name in order to map it correctly but it might happen that another plugin mapped a function to <CR>.
    – TornaxO7
    Feb 18, 2021 at 21:40
  • E.g, in my .vimrc I have a mapping for selecting the current choice in menu with CoC: inoremap <silent><expr> <cr> complete_info()['selected'] != -1 ? coc#_select_confirm() : "\<C-g>u\<CR>". Then in lh-brackets, I override it (to behave as in lexima I think) w/ enrih_imap: call lh#brackets#enrich_imap('<cr>',{'condition':'getline(".")[col(".")-2:col(".")-1]=="{}"','action':'lh#brackets#_add_newline_between_brackets()'},0) Feb 18, 2021 at 23:03
  • 1
    First of all: Thank you for your replys and sorry for the veeeeeeeeeery late reply from me... I'll take a look into your plugin! :)
    – TornaxO7
    Jun 14, 2021 at 23:18

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.