A little Unix difference that irritates: what word erase erases

November 22, 2012

I (still) use a number of different Unixes. For the most part they aren't particularly different in day to day use, but it turns out that there is one little difference that really gets to me. That is what TTY word erase (usually Ctrl-W) erases, or the other way to put it, what it stops erasing at.

In most environments, what C-W considers to be a 'word' is 'everything back to the previous whitespace' (or the start of the line if this is the first word on the line). Suppose that you're typing this:

$ cd /some/whre

The cursor is at the end of the line and you hit C-W; you'll erase all of the /some/whre, going back to the whitespace between the cd and it.

But on some systems, C-W also breaks words at some non-alphanumeric characters; I'm not sure what characters exactly, but they definitely include '/'. On these systems if you hit C-W at the end of our sample line, you will just erase the whre and be left with 'cd /some/' (if you then hit C-W again it erases 'some/', so cleverness is going on).

(This may be a Linux-only behavior, since that's the only thing I can reproduce this on right now.)

It turns out that the second behavior is much more convenient for me. When I'm erasing words, I almost always want to consider the various components of a long path to be separate words; if I want to get rid of the full path, hitting C-W a few times is fast, and if I just want to erase and retype the last component because I've made a mistake in it (as in this example) that's something I can actually do. With the whitespace-only model of word erase it's much more irritating to make a mistake in a path or the like.

Of course this is basically irrelevant to most people these days. This word erase behavior only applies if you're relying on the kernel's handling of line input. Most people are using shells with built in command line editing (and the programs they use will often use readline); in those programs, word erase behaves however the program or library wants it to. I'm an exception because my shell doesn't have command line editing.

(In bash, word erase seems to be 'erase to whitespace'. There is probably some readline control or option for changing this, since readline has a huge collection of customization options.)


Comments on this page:

From 76.171.122.113 at 2012-11-23 12:34:06:

I've been bothered by this for several years, but hadn't thought too much about it as I don't like to do much customization of my environment as I work in quite a few different environments.

I hadn't realized that this was actually bash that changed this (until sometime in the early noughts I was using tcsh not bash). For some reason I decided to see if I could fix this via googling, took me over half an hour to get it to work. It is in fact a readline thing, one can just put this in their ~/.inputrc: "\C-w": unix-filename-rubout BUT that still didn't work for me until I added the following to my .bashrc: export INPUTRC=~/.inputrc (thanks to the guy mentioning that in the comments of: http://www.softpanorama.org/Scripting/Shellorama/inputrc.shtml)

I use vi mode bash editing, and now ctrl-W (in insert mode) erases using / as a delimiter, but in command mode it still does it using whitespace as the delimiter, but I don't think that is a problem as in command mode I would just use normal vi commands.

Note that some people claimed one had to:

do
stty weerase undef
use
set bind-tty-special-chars Off #in .bashrc

but those didn't help me at all. This was with bash 4.2.24 on ubuntu, haven't tried it yet on centos 6.x box.

From 121.44.197.159 at 2012-11-25 05:56:04:

zsh supports the magic variable $WORDCHARS, which is a list of characters ^W et all consider word breaks.

From 195.35.72.170 at 2012-11-27 05:39:56:

I know this is a readline feature, but what does

backward-kill-word (M-DEL)

so ESC - backspace or ALT - backspace give you in your shell?

In my bash shell it gives me exactly what you want.

From 92.236.66.25 at 2012-11-28 13:38:26:

I found the same thing useful, and since I've had these settings in a bashrc snippet:

stty werase undef > /dev/null 2>&1
bind "C-w":unix-filename-rubout > /dev/null 2>&1

..it's done word-delete separated at slashes since.

I've not tried this on anything non-Linux, so I don't know if it's a portable setting to any system which has bash.

Written on 22 November 2012.
« Optional features are in practice not optional to understand
Simple markup as a style guide and limiter »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Nov 22 23:58:14 2012
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.