Skip to main content
4 of 4
added 187 characters in body
saginaw
  • 6.9k
  • 2
  • 27
  • 47

Why does Vim interpret â as Alt-b in a mapping?

I have the following code in my vimrc:

function! s:WordPos(dir) abort
    return a:dir ==# 'right'
    \ ? searchpos('\<', 'nz', line('.'))[1]
    \ : searchpos('\<', 'bn', line('.'))[1]
endfunction

silent! execute "set <M-b>=\eb"
silent! execute "set <M-f>=\ef"

inoremap <expr> <M-b> <SID>WordPos('left') == 0
\ ? '<S-Left>'
\ : repeat('<C-G>U<Left>', col('.') - <SID>WordPos('left'))

inoremap <expr> <M-f> <SID>WordPos('right') == 0
\ ? '<S-Right>'
\ : repeat('<C-G>U<Right>', <SID>WordPos('right') - col('.'))

The goal is to move by words in insert mode with Alt-b, Alt-f without breaking the undo sequence.

It works more or less but if I try to type the character â, instead of inserting it, Vim triggers the mapping for Alt-b, which makes the cursor move one word backward.

As soon as I comment the mapping for Alt-b, the problem disappears. It happens in Vim as well as in gVim.

The output of the shell command xxd -p when I hit Alt-b is 1b62, and for â it's c3a2.

Why does Vim interpret â as Alt-b even though the keycodes seem different?

saginaw
  • 6.9k
  • 2
  • 27
  • 47