Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 19 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, Terminal.app 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? –  shurane 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: Meanwhile, readline is a userspace library originally written for the bash shell (although now used by many programs). It's possible that the other actions were implemented in readline to avoid bloating the tty driver (which already had a considerable amount of flags and toggles, as seen in stty's manual). Also, bash was originally intended for the GNU OS and only later ported to other Unixes; it is much easier to maintain the editing features in a single library than in several very different kernels. –  grawity Apr 17 '12 at 7:44
1  
@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
add comment

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
add comment

Your Answer

 
discard

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.