21

Is there a way to use the alt+backspace in vim command line? It gets unruly when having to backspace /very/long/file/path individually instead of using alt+backspace to delete by words.

sent-hil
  • 18,635
  • 16
  • 56
  • 74
  • 2
    Sorry guys I think I'm using `vim commandline` wrongly, since none of the answers seem to work. I'm talking about the line at bottom of screen which appears when you press `:` in normal mode, the one you type `:set` and other things into. – sent-hil May 18 '11 at 03:43

7 Answers7

29

try using instead <c-w> (that is ctrl+w) to erase words or <c-u> (ctrl+u) to delete lines.

skeept
  • 12,077
  • 7
  • 41
  • 52
  • 4
    Just typing in the same answer :D. For addition Vim's command bar works almost the same as in Terminal (so C-H, C-K, C-N, C-P also works). And if @senthil still prefer Alt + BS then it is `cmap ` – tungd May 18 '11 at 04:09
  • `` and the others works great. Thanks @skeept and @tungd. – sent-hil May 18 '11 at 07:08
  • correct, but not what the op asked for ... chk: https://stackoverflow.com/a/59702513/65706 – Yordan Georgiev Jan 12 '20 at 09:31
3

http://vim.wikia.com/wiki/Map_Ctrl-Backspace_to_delete_previous_word

:imap <C-BS> <C-W>

sets ctrl backspace, i have to look at how to do alt

loosecannon
  • 7,683
  • 3
  • 32
  • 43
1

Vim is unable to receive alt input. skeept's answer seems to be the best alternative.

See this answer:

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
kas
  • 857
  • 1
  • 15
  • 21
1

If you are at the end of the path you can hit B followed by a dW (case matters). This will jump you to the beginning of the word (ignoring the slashes) and subsequently delete the word (again ignoring the slashes).

Hope this helps.

wilbbe01
  • 1,931
  • 1
  • 24
  • 38
  • I see your point here. If I'm already out of insert mode, which pressing opt+ does, then I could use `dW`. This doesn't work for me unless I first go to the [b]eggining of the word by pressing `b`. So the command is: `bdW` – Tony Barganski Apr 07 '22 at 13:49
0

The answer marked as right does not correspond to the behaviour in most UI editors for Alt + BackSpace. The vim shortcut which correspond to this behaviour is db - aka delete back ( a word ?! ), dw would delete word forth, which would be the (Altr or Ctrl ) Del shortcut in most ui programs. Those work basically the same way as the w - move the cursor to the words beginnings and b, move the cursor back to the words beginning ...

Disclaimer: I have used for more than 10 years my .vimrc. , which might have some freaky twist which changes the default behaviour as well ...

Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
0

Sure, it's as easy as:

if has('gui_running')
    imap <M-BS> <C-W>
else
    imap <Esc><BS> <C-W>
endif

The trick here is to know, given a hypothetical foo key, that after pressing a Alt+foo combination, many terminals will send an Escape code followed by foo. Apparently there are exceptions — some terminals do send something that vim can recognize as Alt. But if a imap <M-BS> <C-W> mapping doesn't work for you in terminal, then most likely your terminal sends an Esc instead, so the combination imap <Esc><BS> <C-W> should work for you.

You can read more about that in vim documentation by evaluating :help map-alt-keys

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
-1

x then w should backspace per word as well. d then w will also delete the current word the cursor is on.

Corith Malin
  • 1,505
  • 10
  • 18