19

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
  • 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, 2011 at 9:49

6 Answers 6

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.

3
  • 1
    Wow this may have changed how I use the terminal forever. Thanks!
    – David
    Nov 30, 2012 at 13:52
  • 8
    This does not answer the question
    – Sel
    Jan 18, 2017 at 7:56
  • I am using the combination through ssh with tcsh, it clears everything before the cursor, not just one word before cursor.
    – zyy
    May 16, 2020 at 20:39
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
  • 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, 2011 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, 2011 at 17:34
3

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

"\C-h": backward-kill-word
1
  • 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. Jul 16, 2018 at 19:07
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.

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

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

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.