20

In Linux I'm used to press Ctrl-Backspace to delete the last word but I don't know how to do it in Bash terminal.

In OS X I use Alt-Backspace to do the same thing.

Is there any way of making Bash recognize the Ctrl- or Alt-Backspace key combination to delete last word?

In Linux I use terminator as terminal emulator, in OSX I use iTerm2.

slhck
  • 223,558
  • 70
  • 607
  • 592
licorna
  • 541
  • 2
  • 4
  • 12
  • 1
    Alt-⌫ a.k.a. Alt-← a.k.a. Alt-Backspace works perfectly fine for me. I'm using Terminator 0.93. – Teddy Jun 14 '11 at 09:49

6 Answers6

35

You can always use Ctrl-W. It deletes the word before the cursor and works in every Bash.

See here for a list of Bash keyboard shortcuts.

slhck
  • 223,558
  • 70
  • 607
  • 592
5

You just need to set Option as Meta key in iTerm's preferences (Profiles » Keys).

backward-kill-word is bound to Meta-Rubout (i.e. Opt-Backspace) by default.

The same option exists (albeit less flexible) in Apple's Terminal.app.

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
  • 2
    Actually, you have to use the "+Esc" setting. Only setting it to "acts as: Meta" results in `?` being printed instead of the word getting deleted. – slhck Jun 13 '11 at 17:32
  • @slhck Not for me. But since the developers recommend it anyway, it can't be a bad thing. – Daniel Beck Jun 13 '11 at 17:34
4

You can create a file .inputrc in your home directory and add this content inside:

"\C-h": backward-kill-word
Sel
  • 253
  • 1
  • 6
  • 2
    Note that this and `C-w` in the accepted answer differ by what is considered a "word". `C-w` will delete from the cursor position until it encounters non-space characters and then a space (i.e., `"this is a/se=weird |test"` where `|` marks the cursor position will yield `"this is test"`). In contrast, the `C-h` bind here (which can be bound using `bind '"\C-h": backward-kill-word"` in a `.bashrc`) would yield `"this is a/se= test"`, as it basically delimits words on what a bash variable would, not just spaces. – jeremysprofile Jul 16 '18 at 19:07
2

As some have mentioned, this may also depend on your terminal emulator. For example in KDE's Konsole, you can define the behavior of Ctrl+Backspace in the key bindings settings of the profile:

Set Backspace+Ctrl to the output \E\x7f (default \b)

Konsole Key Binding List

Dan Dascalescu
  • 3,769
  • 6
  • 35
  • 53
quazgar
  • 646
  • 6
  • 9
2

In terminator you can use the following "plugin", along with

[keybindings]
    kill_word = <Control>BackSpace

in your config file. This will map Ctrl-Backspace to ESCDEL, another shorcut to delete last word.

0

You can just use Alt-DEL instead of the normal Strg-DEL.

Luca Steeb
  • 101
  • 2