1

In raw mode, if you type a character in the terminal, the application on the other side will read the ASCII code for the typed character.

But what about when you press the backspace key in the terminal, what would the application on the other side read?

1 Answers1

2

Actually, if you type a character in a terminal, the application will read that character.

Well, more precisely, if you type a character in a terminal, it's converted to one or more bytes — most modern Unix system use the UTF-8 encoding of characters. The application reads those bytes and reassembles the characters. This is still not ASCII codes — ASCII is a 7-bit encoding, so all characters in the ASCII character set fit in one byte.

When you type a function or cursor key such as BackSpace, Tab, Return, F1, Left, etc., it's encoded as a control character or an escape sequence. There are a few control characters that correspond to function keys, such as ^I (byte value 9) for Tab and ^M (byte value 13) for Return. Most other function keys send an escape sequence beginning with the escape character (^[, byte value 27).

BackSpace sends a control character. For historical reasons, which control character it sends depends on the terminal and on its configuration: it can be either ^H (byte value 8) or ^? (byte value 127). On many modern terminals, you can change this in the configuration; see How to allow backspaces in unbuffered/non-canonical mode?. In case the setting isn't picked up automatically, you can declare it with stty.

For more background, see How do keyboard input and text output work? and How to make a comprehensive set of possibilities for defining GNU-screen "command characters"?

  • No, stty is not how one changes the terminal's behaviour. See my answer to the duplicate question, where this has already been answered including the correct way, for only some terminals, that one can change what the key sends. – JdeBP Nov 06 '17 at 07:02
  • stty erase is to tell the line discipline which character is sent by your terminal upon the Backspace key. However, the line discipline doesn't use that information when in raw mode. That information is used to implement the LD's line editor (whereby upon receiving that character, it removes the last character from the line buffer as opposed to adding the ^H/^? to that edit line buffer, and the content of that buffer made available for reading by the application when ^J, eof, eol, eol2 characters are received). – Stéphane Chazelas Nov 06 '17 at 11:42