17

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.

  • 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 9:49
31

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.

  • 1
    Wow this may have changed how I use the terminal forever. Thanks! – David Nov 30 '12 at 13:52
  • 3
    This does not answer the question – Sel Jan 18 '17 at 7:56
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.

  • 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
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.

  • Unfortunately this stopped working in terminator 1.9. Could you provide a fix by any chance? – Sel Apr 18 '17 at 8:38
  • 1
    Sorry, stopped using terminator long time ago... – OlivierBlanvillain Apr 19 '17 at 14:14
2

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

"\C-h": backward-kill-word
  • 1
    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
0

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

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.