3

I have a map which allows me to navigate with hjkl in insert mode:

" In insert or command mode, move normally by using Ctrl
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
cnoremap <C-h> <Left>
cnoremap <C-j> <Down>
cnoremap <C-k> <Up>
cnoremap <C-l> <Right>

All maps works, except <c-h>. In insert mode, when a I press <c-h>, backspace occours. I know that ^H is the ASCII for backspace, but I would like to know if there's some way to make my <c-h> works as <left>.

When I run: :verbose inoremap <c-h> it returns

i  <C-H>       *@<C-R>=AutoPairsDelete()<CR>                                                                                                                            │ost/site/content/themes/sc5
        Last set from ~/.vim/plugged/auto-pairs/plugin/auto-pairs.vim line 549                                                                                          │0k/gulpfile.js
i  <C-H>       * <Left>                                                                                                                                                 │[10:29:37] Starting 'watch'
        Last set from ~/.dotfiles/nvim/init.vim line 264

A courious thing is that in command mode, <c-h> works fine!

I'm using iTerm2 in MacOS Catalina with tmux and neovim.

1 Answer 1

4

Thanks to your output of verbose inoremap <c-h> it's really easy to see what is happening here!

You are using the auto-pairs plug-in and it's installing buffer-local mappings for the bracket keys, but also for keys such as Enter and Backspace. It maps backspace so it can "undo" a close bracket action if you type backspace to erase a mistake of yours.

Now, there's actually a difference between <C-h> and "backspace". While historically the backspace key would produce the <C-h> sequence, that's no longer the case and most modern terminal emulators will produce a different sequence for the "backspace" key (unless they're configured to produce the <C-h> sequence for backward compatibility reasons, but that's more and more rare nowadays.)

For historical reasons, auto-pairs (and probably most other plug-ins that map backspace) will by default map <C-h> as well as "backspace", so that if you have an old terminal or have it misconfigured, everything will keep working the same with <C-h> as it does with the "backspace" key.

(It's true that the default action of Vim for <C-h> is the same as backspace, for much of the same reasons.)

Fortunately, auto-pairs allows you to control these two mappings individually, by setting global variables from your vimrc, you have g:AutoPairsMapBS to create the mapping for the backspace key, and g:AutoPairsMapCh to create the mapping for <C-h>.

So you can fix your issue simply by adding this line to your vimrc file:

let g:AutoPairsMapCh = 0
4
  • 1
    Hello @filbranden, tks your great explanation and the solution. You helped my productivity a lot! Greetings from Brazil! Commented Apr 21, 2020 at 15:04
  • 1
    @DiegoSomar Bem-vindo ao site do Vim! Tem alguns outros brasileiros por aqui, de repente fundamos um site em português também! 😁
    – filbranden
    Commented Apr 21, 2020 at 15:13
  • 1
    Muito bom saber disso! :) Commented Apr 21, 2020 at 15:57
  • 1
    This saves my day. The plugin is exactly the culprit in my case. Commented Nov 29, 2022 at 17:13

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.