25

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.

Stefan M
  • 285
  • 3
  • 7

3 Answers3

42

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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • This is not only the best answer but extremely informative, thank you! – Stefan M Nov 20 '10 at 06: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 02:45
  • 1
    @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`](http://www.freebsd.org/cgi/man.cgi?query=stty&manpath=Unix+Seventh+Edition) looked like.) – u1686_grawity Apr 17 '12 at 07:44
  • 1
    @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](http://en.wikipedia.org/wiki/GNU_operating_system) 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. – u1686_grawity Apr 17 '12 at 07:44
  • 3
    @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). – u1686_grawity Apr 17 '12 at 07:49
  • 4
    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
20

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
fjarlq
  • 299
  • 2
  • 6
4

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.

TCAllen07
  • 141
  • 3
  • It took me a long time indeed to figure this out, and this solution works indeed on OS X (with iTerm2) when logging in on a remote bash shell. Thanks! – Martijn Oct 09 '17 at 17:52