I have a function that depends on the character under the cursor : if it's a ), then the function does nothing ; if it's not, the function adds a ).function! Ket()   let char = getline(.)[col('.')-1]   if char != ')'      execute normal! a)\<esc>   endifendfunctionIf I call it from normal mode, using :call Ket(), it works fine.But I have it remapped from insert mode, as follows :inoremap ) <Esc>l:call Ket()<CR>a. This mapping should (from my understanding) :escape insert mode with <Esc>,go one character to the right with l (because escape to normal mode moves the cursor to the left), call my Ket() function,get back to insert mode (a)If I am in insert mode, and my cursor is on a ) character, then I get what I expect by pressing ) : the cursor moves to the right, and I end up in insert mode.But if my cursor is on any other character, pressing ) only escapes insert mode, as if I had pressed Esc.
Function does not work when called as from a mapping
vimscript;insert mode;normal mode;functions
Instead of leaving insert mode you could try to do the same without the need for a function (because in this case it's a simple case):inoremap <expr> )  strpart(getline('.'), col('.')-1, 1) == ) ? \<Right> : )Decomposing:inoremap <expr> )  will insert the result of the mapping as instead of )strpart(getline('.'), col('.')-1, 1) get the current character under cursor== ) ? \<Right> : )if it's a ) insert the right key, if not, insert a ).If you want to keep your function, you can do it as well like this:function! Ket()  if strpart(getline('.'), col('.')-1, 1) == )     return \<Right>   else    return  )  endifendfunctioninoremap <expr> ) Ket()