The problem is that many terminal emulators emit a <C-h>
(0x08) when you press <Backspace>
, there is no way for Vim to see if the user pressed Backspace, or <C-h>
.
So one thing you can do, is remap the <Backspace>
key:
inoremap <Backspace> <Esc><C-W><C-h>a
inoremap <C-l> <Esc><C-W><C-l>a
This should work are you expect, with the obvious downside that you've now lost your backspace key.
Not all is lost, there is also another common keycode for the backspace key: <C-?>
(0x7f, or the DEL
character). So you'll need to configure your terminal emulator to send that key instead.
For xterm
setting xterm.*backarrowKey: false
in ~/.Xdefaults
is usually enough, though the logic that xterm uses in deciding which character to send is quite complex, see the backarrowKeyIsErase
in xterm(1)
for more information.
You need to load this file with xrdb ~/.Xdefaults
and open a new terminal window for this to take effect.
For other terminal emulators, the procedure will differ. For example in gnome-terminal
you can configure this in Edit -> Profile preferences -> Backspace key generates
.
Next, you want to tell Vim to accept ^?
as the backspace character with:
set t_kb=^?
Don't type ^?
, you need to insert the escape code, so use <C-v>x7f
to insert it (or, if you've reloaded the terminal settings, pressing <C-v><BS>
should also work).
You should now be able to remap <C-h>
:
inoremap <C-h> <Esc><C-W><C-h>a
inoremap <C-l> <Esc><C-W><C-l>a
Related: Mapping Ctrl+s does not work