[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: backspace and delete



On Tue, Aug 07, 2001, Tzafrir Cohen wrote about "backspace and delete":
> I've tried following that, and got into a mess. For instance, if I run
> less, then pressing Backspace simply prints '^H', and pressing Delete
> deletes backwards .
> 
> Is there any good reason for this mess? Can anybody recommend a better
> source of information?

The root of all evil is the fact that Unix applications that run on a tty
(or pseudo-terminal, or whatever the modern terminology for that is) only
get input as 8-bit ASCII bytes. This means that the key labeled "backspace"
can't send some sort of fancy 32 bit keycode, or anything - an ASCII character
must have been chosen for that character to generate.

The character ASCII (or whomever later defined the "control characters" 0-31)
chose was ^H, i.e., control-H, i.e., the character with decimal code 8 (octal
code 010). Most standard terminals used to produce that character when the
backspace key was pressed.

But the other side, the tty "line discipline" or the application itself,
also needed to know which ASCII character signifies a backspace, so that
when that character was pressed one character is erased from the input buffer
(when the tty is in "cooked" mode - more on that below). The Unix kernel
holds for each tty a list of flags manipulated through ioctl(), or through
the stty(1) user command, and among them is the "erase character". So doing

	stty erase ^H

tells the tty (if it is in "cooked" mode, where the tty passes an edited
line to the application only after the press of a newline), or the applications
(if the tty is in "raw" or "cbreak" mode and the application is passed every
character as soon as it is typed) which ASCII input character should be
treated as an "erase" character.

So where's the problem?

The problem is that some keyboards also had another key, named "delete".
or "rubout". Originally the Unix standard was for this "delete" key to output
the ^? (decimal 127, octal 0177) character. This "delete" was usually used to
cause the tty to interrupt (kill -2) the current process: again, this is
defined with stty:

	stty intr ^?

But then the confusion starts. Somewhere along the making of DOS popular,
and the fact that some keyboards didn't have a "delete" key, and the fact
that DOS applications used the "delete" to delete a character (not kill
the process), people started wanting the key combination ^c (control C)
to be the interrupt key, and did
	stty intr ^c
and they also wanted the "delete" key to back over a character (what's
wrong with backspace? don't ask me...), and since the delete character
was already sending ^? (this was preset in many terminals and terminal
emmulators) they did
	stty erase ^?

All the mess starts when your terminal (or, in the modern world, a terminal
emulator like "xterm") sends one character for erase, while your kernel and
applications know (via stty) that another character is the erase character.

To return everything to a consistent state, you simply have to find out
what your backspace key generates, and use the apropriate stty command. For
example, do
	stty erase ^V<BACKSPACE>         <- press control-V and then backspace
        stty intr ^c                     <- or delete, or whatever you prefer.


Here's what I prefer to do on my xterms (which, let's face it, is the terminal
emulator I use 99% of the time): I prefer to have xterm generate a ^H character
for backspace, ^? character for delete, and then tell stty of these characters.
You can do all that with the following definitions in your ~/.Xdefaults (note
that this part of xterm's configuration changed considerably last year, so
this will work verbatim only on a modern xterm):

XTerm*VT100*backarrowKey: true
XTerm*VT100*deleteIsDEL: true
XTerm*VT100*ttyModes: intr^? erase^H

Other terminal emulators should have similar configuration parameters.
But again, these are not necessary: you can live with whatever character
the terminal emulator generates (but only if it generates one character and
not a multi-character escape sequence!!) and teach the kernel and applications
to recognize it, using the stty command.

I hope this "article" helps ;)

-- 
Nadav Har'El                        |         Tuesday, Aug  7 2001, 18 Av 5761
nyh@math.technion.ac.il             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Help Wanted: Telepath. You know where to
http://nadav.harel.org.il           |apply.

=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il