13

How can I remap alt+backspace to delete words like native *NIX text manipulation? I checked out this thread: Using alt+backspace key in vim command line to delete by words

And the examples like: cmap <a-bs> <c-w> and :imap <A-BS> <C-W> don't do anything. And the accepted answer was actually to not even remap it, but to use ctrl+w. Since VIM's alt+backspace doesn't do anything I'd rather remap it to something I'm used to.

I'm using terminal based VIM (specifically in iTerm)

Community
  • 1
  • 1
Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
  • 3
    The first step is to find out what your terminal does with the key combination. Start vim, go into insert mode, type Ctrl-V followed by backspace, then another Ctrl-V followed by Alt-backspace. If it prints the same thing for both, you're out of luck. Otherwise, the map command should work if you type Ctrl-V Alt-Backspace in the `:map` command instead of ``. – Alan Curry Jul 25 '12 at 22:47
  • 1
    visual mode then backspace highlights 1 character. Visual mode then `alt+backspace` gets out of visual mode, so they don't do the same thing. Also, I don't want to be in visual mode to do `alt+backspace`. That defeats the ease of deletion i was looking for (like `dd`), so, now what? – Oscar Godson Jul 25 '12 at 22:54
  • I didn't want you to be in visual mode either. Ctrl-V in insert mode is not supposed to go into visual mode. You must have a weird mapping (`:imap `) or you weren't in insert mode when you did it. – Alan Curry Jul 25 '12 at 23:06
  • Oh, i see. I wasn't in insert mode. Skipped that. So, in insert mode and pressing ctrl+v then backspace I get `^?` and doing ctrl+v in insert mode then alt+backspace doesn't really do anything. Just moves the cursor back 1 position. – Oscar Godson Jul 25 '12 at 23:39
  • Well, it's good news that they do different things, so there's a chance of distinguishing them. But I don't know what to do when Ctrl-V doesn't protect a special key. Maybe try typing Ctrl-V Alt-Backspace elsewhere - at the vi `:` prompt, at your shell prompt, inside a `cat >/dev/null` command... it must be sending something to the terminal, we just don't know what it is. – Alan Curry Jul 26 '12 at 00:02
  • Maybe [this thread](http://stackoverflow.com/questions/196357/making-iterm-to-translate-meta-key-in-the-same-way-as-in-other-oses) can offer some assistance on the meta key in iTerm? – Conner Jul 26 '12 at 01:18
  • So, @AlanCurry, doing it at the `:` just does `^` for for `ctrl+v` then doing `alt+backspace` clears that out – Oscar Godson Jul 26 '12 at 05:09
  • @Conner I already have my meta key set to alt so I can do things like `alt+b` and `alt+f` to jump back and forwards between words – Oscar Godson Jul 26 '12 at 05:09
  • You know all these keybindings you're talking about are emacs-based, right? Are you sure you want to use vi[m] at all? How about the bash prompt? If Alt-Backspace works there, see how it likes it with a Ctrl-V. – Alan Curry Jul 26 '12 at 05:35
  • @AlanCurry, your suggestion of doing `Ctrl-V backspace` and `Ctrl-V Alt-backspace` and then doing `:map` worked perfectly for me (OSX iTerm2 vim). If you posted it as an answer I'd definitely upvote it :) Thanks! – A.Wan Sep 03 '15 at 23:50

3 Answers3

9

On macOS with iTerm2, I have the option keys mapped to +Esc (like many people), and I found that pressing Option+Backspace actually was interpreted by vim as an Escape press followed by a Backspace press, so the following binding worked perfectly for me; I recommend trying it even if your configuration is different than mine, just in case it works for you!

:imap <Esc><BS> <C-w>
Christopher Shroba
  • 7,006
  • 8
  • 40
  • 68
  • 1
    Thanx @Chrisopher After searching for hours and trying many things, this is what worked for on catalina with zsh. Added in vimrc `imap . – userx Dec 08 '20 at 12:00
  • 1
    Just want to say thank you, I've been looking for this for dayz – tga Oct 04 '21 at 14:36
5

The Alt/Meta key is problematic in Vim and most terminals, see this answer of mine for an overview of the situation (the situation is the same for Meta and Alt).

In short, Vim doesn't receive Alt at all: hitting Alt+Backspace is exactly the same as hitting Backspace.

Anyway, it will be better for you in the long term to learn and get accustomed to Vim's default key-mappings.

Community
  • 1
  • 1
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    I already am, but as with all custom mappings it's to make it easier or else there wouldn't be a way to make custom mappings and overrides :) – Oscar Godson Jul 26 '12 at 22:05
  • Yes and no. I see custom mappings as ways to augment Vim, mainly. Anyway, the Alt key doesn't exist for Vim so you'll have to find another mapping (which defeats your purpose) or get used to ``. – romainl Jul 27 '12 at 06:01
  • 1
    I am used to having to use it, it's just super awkward finger placement on a Mac laptop. – Oscar Godson Jul 27 '12 at 14:49
  • 1
    For me Vim sees which makes the behavior pretty easy to explain (exit insert, move left one). – Steven Lu May 22 '13 at 21:28
  • 1
    "It will be better for you" is subjective. Don't make assumptions about others. "My recommended alternative" is a more appropriate beginning to that last sentence. – Sridhar Sarnobat Oct 14 '20 at 22:15
  • @SridharSarnobat no it is not subjective at all. Learning one's tool properly is always better than not learning it. – romainl Oct 15 '20 at 05:25
1

If you are on OSX, macvim uses the standard key bindings, so pressing Alt+Backspace will delete the entire word. Same goes for navigating between words with Alt+RightArrow and Alt+LeftArrow.

Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87