Binding backward-kill-word to Ctrl+w

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 Terminal.app on my Macs at home this does not work. Same goes for any FreeBSD or Linux server I happen to be logged into from Terminal.app.

So I turn to superuser for help.


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, Terminal.app also has a similar setting to control the behavior of Meta/Alt/whatever-Macs-use.


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
Control-u: kill-whole-line
Control-w: backward-kill-word