Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to switch from prolonged use of Tcsh to recent exploration of Bash.

I've managed to port over all my favorite features, except for Ctrl+w which treats spaces and slashes as word boundaries, most likely backward-kill-word. In Bash however readline deletes all the way to the first space, deleting all slashes between.

I've tried many various combinations of \C-w: backward-kill-word in both .inputrc and .bashrc using bind but I can't get it to work the way I want.

Funny enough, through Putty from Windows at work I can use Alt+Backspace, which also the manual says is the default binding, to produce the exact behavior I want. But in on my Macs at home this does not work. Same goes for any FreeBSD or Linux server I happen to be logged into from

So I turn to superuser for help.

share|improve this question
up vote 30 down vote accepted

readline does not bind over Ctrl-W since it is handled by the terminal driver by default:

$ stty -a
(...) werase = ^W; (...)

To disable this, you have to run (in ~/.bashrc):

stty werase undef
bind '"\C-w": backward-kill-word'

.inputrc will not help here since it is read when Ctrl-W is still assigned to werase.

Also note that \C-w format keyseqs must be enclosed in double-quotes, as above.

Different terminals may handle Alt-Backspace differently.

  • PuTTY and GNOME Terminal (with default settings) send 1b 7f (ESC DEL)
  • Xterm with metaSendsEscape: true sends 1b 08 (ESC BS).
  • Xterm without metaSendsEscape sends 88 (HTS, or BS with highest bit set).

Xterm has a few settings to control the behavior of Alt key (called Meta in Xterm and X11 in general). With metaSendsEscape, holding Alt will prefix the keypress with an ESC (hex 1b). Without the setting (default mode), Alt will set the highest bit of the input character, resulting in special characters. (For example, Alt-N results in ESC n and î respectively.)

As far as I can remember, also has a similar setting to control the behavior of Meta/Alt/whatever-Macs-use.

share|improve this answer
This is not only the best answer but extremely informative, thank you! – Stefan M Nov 20 '10 at 6:56
This is fantastic! Is there a reason that some keys are bound in stty and others in readline, though? – Ehtesh Choudhury Apr 17 '12 at 2:45
@shurane: The werase "word erase" action, like normal erase (Backspace), is implemented by the OS itself, in the "tty device" driver -- notice that they are available even in programs that just blindly read from stdin, e.g. cat. On all Unixes, stty is used to control various tty parameters including these special keys. (Here's how UNIX 7th Edition stty looked like.) – grawity Apr 17 '12 at 7:44
@shurane: Technically, however, all these actions, including word erase, are implemented by readline -- when active, it completely disables Backspace and Ctrl+W handling by the terminal driver. readline just respects the stty configuration for simplicity, but it can be overriden by readline's own settings (as described in Matt Day's answer). – grawity Apr 17 '12 at 7:49
This is a great answer for information, upvote. However, it should be noted that this disables Ctrl-w for all programs unless they use readline, which is probably not what anyone wants..., so try @fjarlq 's answer. – 0fnt Jul 13 '15 at 16:53

grawity wrote that this cannot be controlled in the .inputrc file, but that is incorrect.

If you set bind-tty-special-chars off in your .inputrc, you can then customize the behavior of the special terminal chars.

For example:

set bind-tty-special-chars off
"\C-u": kill-whole-line
"\C-w": backward-kill-word
share|improve this answer
It works. Thanks! – Domon Aug 11 '14 at 10:16

For the record, the answer as it applies to iTerm2 is to set Alt-Backspace (or Opt ⌥- on Mac) to send Hex Codes 0xb1 and 0x7f in order to perform the backward-kill-word action which treats non-alphanumeric characters as delimiters.

For distinction, here's the behavior I'm describing, in which pressing Opt ⌥- one time changes this:

echo "/example/filepath/with_non-alpha.characters@blah:meh

to this:

echo "/example/filepath/with_non-alpha.characters@blah:

And subsequent presses of Opt ⌥-:

echo "/example/filepath/with_non-alpha.characters@

echo "/example/filepath/with_non-alpha.

echo "/example/filepath/with_non-

echo "/example/filepath/with_

echo "/example/filepath/

Here's what the setting looks like in iTerm2: enter image description here

So, specific to Putty and Terminal, grawity's answer is great. But having spent an embarrassing amount of time looking for how to apply the setting in iTerm2, this superuser post seems to come up most frequently, so it seemed responsible to provide the relevant info as it applies to iTerm2 as an Answer attached to this Question.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.