0

how do I remap backspace combinations? I can remap backspace itself in normal mode, but not in insert mode, and I cannot map combinations:

" this works:
nmap <BS> dd

" this doesn't work:
imap <BS> <Esc>dd
nmap <C-BS> dd
imap <C-BS> <Esc>dd

I'm using Vim 7.4 inside Terminator.

  • 1
    Possible duplicate of Mapping Ctrl with equal sign – Herb Wolfe Nov 21 '17 at 15:49
  • I don't this is an exact duplicate of that question. <C-BS> is actually a special case of <C-Key> since it will usually send ^H on a normal keypress and ^? on <C-BS> (or vice versa, depending on your terminal). – Martin Tournoij Nov 22 '17 at 20:42
3

You need to first press ctrl+v in insert mode before pressing ctrl+backspace to insert the terminal code of ctrl+backspace.

Your mapping will look like this:

inoremap ^_ <Esc>dd

See :h i_CTRL-V:

CTRL-V      Insert next non-digit literally.  For special keys, the
            terminal code is inserted.

However you mapping for <BS> in insert mode should work (it works fine on my setup) maybe you should try the non recursive version of the mapping:

inoremap <BS> <Esc>dd
1

This all works fine in gVim, so I'm presuming you're using terminal Vim.

It's hard to say definitively what is happening in your setup without more information, because the way Backspace works depends on your terminal configuration. However, note that BS is a control character, and the way that control characters were implemented in traditional terminals was by pressing the Ctrl key plus another key.

As such, it's entirely possible that your terminal is already sending Ctrl-H when you press backspace: if this is the case pressing the Ctrl key at the same time will have no effect.

Therefore, I suspect statox's answer won't work for you: for me it just results in a mapping of an unmodified <BS>. (Whereas nmap <C-BS> results in a correct mapping of Ctrl-Backspace that cannot be accessed via key presses!)

Note that, in general, there are a limited number of reliable Ctrl-mappings you use in Vim. See the this answer to a related question and the FAQ entry it links to for more details.

Therefore, I'm afraid that the real answer to your question is, "Don't do that."

1

My experience in terminal Vim is the same as noted by Rich; trying to use Shift and Alt were equally fruitless. So this isn't a direct answer, because it appears no one-size-fits-all answer exists due to technical limitations.

In the end I compromised by using <Leader> as the modifier (usually set as \). I should note that this doesn't work quite the same as a normal modifier key combo, as you must strike \ before each press of Backspace. With the caveats above in mind, I think these mappings should broadly work for you:

inoremap <Leader><BS> <C-o>dd
nnoremap <Leader><BS> dd

You may also want to look at your timeoutlen value, depending on how long you want the Leader to wait for your next key strike.

  • 1
    Note that you probably want to use inoremap and nnoremap here to avoid recursive mappings. See here for more details. – statox Nov 29 '18 at 10:19
  • 1
    thanks for the pointer, I've updated my answer – bxm Nov 29 '18 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.