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>"
endif
endfunction
If 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.
:h :map-<expr>
for a better approach. Keep in mind however that insert-mode maps are tricky, and what you're trying to do is a solved problem. Something like delimitMate or auto-pairs will do a much better job at handling corner cases than your naive map.