8

I tried mapping control-H, J, K, and L respectively in insert mode to move around splits.

inoremap <C-h> <ESC><C-W><C-h>i 
inoremap <C-l> <ESC><C-W><C-l>i

When I press control L, it moves along fine, but when I press control H, it does a backspace.

My understanding is that control-H is backspace in terminal, which is why I can't do this. Is there any workaround? The following says that control h is not mapped

:verbose imap <C-h>
1

1 Answer 1

7

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

3
  • There actually is a way to differentiate these keypresses in some terminal emulators (namely xterm and pangoterm). The latter does it by default and xterm can be configured to do so.
    – jamessan
    Commented Apr 29, 2015 at 13:16
  • @jamessan Thanks; do you happen to have some info on how to do this in xterm? Commented Apr 29, 2015 at 20:18
  • I used your gnome terminal instructions, so now the backspace key generates a "ASCII delete", and the backspace key still works fine. That part is good. The problem is, I can't seem to override the mapping of control h, I suspect because it still tries to handle it as a backspace-like thing
    – matts1
    Commented Apr 30, 2015 at 5:01

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.