Using Ctrl or Alt AND backspace to delete last word in Bash

17

7

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.

licorna

Posted 2011-06-13T16:43:04.270

Reputation: 471

1Alt-⌫ a.k.a. Alt-← a.k.a. Alt-Backspace works perfectly fine for me. I'm using Terminator 0.93. – Teddy – 2011-06-14T09:49:12.043

Answers

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.

slhck

Posted 2011-06-13T16:43:04.270

Reputation: 182 472

4This does not answer the question – Sel – 2017-01-18T07:56:16.677

1Wow this may have changed how I use the terminal forever. Thanks! – David – 2012-11-30T13:52:57.450

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

Posted 2011-06-13T16:43:04.270

Reputation: 98 421

2Actually, 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 – 2011-06-13T17:32:47.593

@slhck Not for me. But since the developers recommend it anyway, it can't be a bad thing. – Daniel Beck – 2011-06-13T17:34:15.267

2

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

"\C-h": backward-kill-word

Sel

Posted 2011-06-13T16:43:04.270

Reputation: 203

1Note 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 – 2018-07-16T19:07:54.123

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.

OlivierBlanvillain

Posted 2011-06-13T16:43:04.270

Reputation: 71

Unfortunately this stopped working in terminator 1.9. Could you provide a fix by any chance? – Sel – 2017-04-18T08:38:15.447

1Sorry, stopped using terminator long time ago... – OlivierBlanvillain – 2017-04-19T14:14:51.523

0

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

Luca Steeb

Posted 2011-06-13T16:43:04.270

Reputation: 101