Screwtape's Notepad

What should the backspace key do? Ctrl-H vs. DEL

Many young programmers, when writing their first program that runs in a terminal, are surprised to discover that when they press the key marked “Backspace” on their keyboards, the byte their program receives is $7F, ASCII “Delete”, not $08, ASCII “Backspace”. Alternatively, they may be surprised that the key marked “Delete” produces ESC [ 3 ~ rather than ASCII “Delete”.

Shouldn’t the keys produce the codes that bear their name? After all, the A key produces $41, ASCII “A”, and the space key produces $20, ASCII “Space”.

There are documents like the Debian Policy Manual that require things be set up this way, but they don’t explain why, except perhaps “so that Emacs can use Ctrl-H for the Help command”, which doesn’t seem very persuasive.

As usual, the answer turns out to depend on history.

The ASCII character set was designed (among other things) to control something like an electric typewriter. So there’s a code to move the print-head all the way back to the left of the page ($0D, “Carriage Return”), a code to advance the paper by one line ($0A, “Line Feed”), a code to advance the paper all the way to the beginning of the next page ($0C, “Form Feed”), and so forth.

The earliest terminals basically were souped-up electric typewriters, so this worked out fine. Later terminals replaced the paper with a CRT display, but they were designed to be backwards compatible, so they kept using ASCII. This was a little awkward, though, since the paper-specific commands like “Form Feed” didn’t really make sense on a CRT. Likewise, there were operations the CRT supported that weren’t available in ASCII, like scrolling up, or replacing text. As a result, these “video terminals” had extra “control sequences” to activate their special features (usually beginning with $1B, “Escape”) in addition to making what use they could out of the standard ASCII codes.

Here’s the keyboard of the VT100, a video terminal introduced by the Digital Equipment Corporation in 1978, and one of the first to conform to the ECMA-48 standard for terminal control sequences:

An overhead photo of a VT100 terminal keyboard

It has a lot of the same keys you’d see on a modern computer keyboard, but also some others! Luckily, somebody has made the VT100 manual available online, so we can see what those keys do:

Probably a given computer would use Carriage Return or Line Feed to signal the end of a line, not both, but a terminal could be plugged into any computer, so it supported both. Likewise, a given computer would probably use Backspace or Delete, not both, but again - best not to make assumptions.

Note that there are also non ASCII keys, like the cursor keys at the top of the keyboard, and the function keys PF1-4 above the numeric keypad. These keys send “control sequences” rather than individual ASCII codes. At the time this terminal was designed, no software understood these sequences, but DEC included them anyway, in the hope that the having keys not limited to existing ASCII functions would be useful enough that software would be updated to support them.

It turned out they were exactly right. The VT100 was wildly popular, and software made extensive use of the VT100’s control sequences rather than being limited to standard ASCII.

Here’s the keyboard of the VT220, the VT100’s successor, introduced in 1983:

A photo of a VT220 terminal keyboard

Just looking at the thing, we can see there are a lot more keys, which all send different and custom control sequences rather than specific ASCII codes.

Again, the VT220 manual is available online, so we can see what the keys do:

DEC had switched almost entirely from “here’s an ASCII keyboard with some control sequences if you’d like” to “here’s a control-sequence keyboard with some ASCII for compatibility”. If the VT100 was popular, the VT220 was mega-popular, so when micro-computers started replacing terminals, the VT220 was the terminal they tried to be compatible with. It also helps that the VT220 keyboard inspired the 1985 IBM Model M keyboard, so PCs had about the right number of keys in the right locations to properly emulate the VT220.

So that’s why the “Backspace” key on your keyboard sends $7F Delete rather than $08 Backspace: specifically because your terminal emulator emulates the VT220, not the VT100, but more generally because basic ASCII is too limited for controlling a video terminal, and the flexibility of control sequences is almost always worth the compatibility cost.