Re: delete key printing ^H

Shawn Bayern (shawn.bayern@yale.edu)
Tue, 30 Mar 1999 17:50:45 -0500 (EST)

On Tue, 30 Mar 1999, Matt McClure wrote:

> I had checked that out already, but it didn't answer all my questions.
> What I'm really wondering is how bash, for example, can handle both ^H
> and ^?, echoing neither to stdout, but using both to delete the last
> character typed (or do whatever you set it to do using the builtin
> bind)?

Bash, like most modern shells, accesses the terminal in 'raw' mode so that
it can process individual keystrokes. (This is in contrast to 'cooked'
mode, which you're probably familiar with: in cooked mode, when you
getchar(), you don't actually get a character until a newline is
encountered and the line is sent to the program for processing.)

> Using stty can make it so that one of the two doesn't get echoed to
> stdout, but is there a way to make it so that neither gets echoed, and
> both erase a character... or better yet, so that, in all command-line
> programs, backspace deletes the last character typed and delete erases
> the character in front of the cursor?

You could wrap your entire environment in something, like expect, that can
process individual keystrokes as they come in. Obviously, that's not
ideal. (There's possibly some way to do this in emacs, which would make
some people happier than 'expect', but I wouldn't know.) Most people are
happy with choosing one character to be the terminal's 'erase' key
(meaning 'delete previous character and move the cursor back') and
ignoring (that is, not pressing) all the others.

> I guess a separate question is how to write programs so that they echo
> neither ^H nor ^?. How does bash avoid it when perl, run from within
> that shell, echoes one or the other?

See the bash source. :) More helpfully, look at the man page for
termios(3). You can also use ncurses, of course, but that might be more
involved than what you're looking for.

Shawn