2

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.

2
  • 1
    Possible duplicate of Mapping Ctrl with equal sign
    – Herb
    Nov 21, 2017 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). Nov 22, 2017 at 20:42

4 Answers 4

4

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.

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

You can get the usual ctrl-backspace Windows behaviour (deleting previous word) following this Vim tip:

https://vim.fandom.com/wiki/Map_Ctrl-Backspace_to_delete_previous_word

You just have to put these lines on your .vimrc

" Map Ctrl-Backspace to delete the previous word in insert mode.
imap <C-BS> <C-W>

Alternatively, you can put the following lines on your .vimrc to make the behaviour more readline-ish:

inoremap <C-w> <C-\><C-o>dB
inoremap <C-BS> <C-\><C-o>db
1
  • Welcome to Vi and Vim! A little explanation goes a long way, especially to prevent link rot.
    – D. Ben Knoble
    Apr 10, 2021 at 15:55

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.