Hacker News new | ask | show | jobs
by saurik 1297 days ago
So, to be fair, C-h isn't some crazy invention of readline (and thereby a failure of what it was "advertising") that you got used to: it is the standard key code for backspace. Just like a horizontal tab is ASCII 9 and a carriage return is ASCII 13, ASCII 8--and I realize this might make less sense as this in some sense "undoes" a thing and maybe you would never expect it to be in a file--is backspace.

Meanwhile, all Control does is convert a normal (capital) character into a control character, which is the term for the ASCII characters below 32 (aka, the space character) by essentially subtracting 64 (which is almost but not quite equivalent to masking out a bit). Notably, @ is 64, so C-@ generates ASCII 0, which is why you see ^@ printed for NUL bytes in many editors (including vim). After @, you get A-Z and then [, \, ], ^, and _.

This is why, if you want to get Escape, you can type C-[, as ESC is 27 and [ is 91. And this is also how C-G becomes that annoying bell, because G is the 7th letter of the alphabet and BEL is 7, And so, C-M is equivalent to pressing Enter, C-J is equivalent to pressing Tab... and C-H is equivalent to pressing Backspace!! Now, there is also a DEL, for the Delete key, which is which is 127. As ASCII is designed for 7-bit, that is the same as -1, so you can type it using C-?, as ? is 63 ;P.

OK, so, one might should then wonder how the hell Emacs even works, if they remapped the character code for Backspace to do something silly like Help... right?! And the answer is kind of "it doesn't", which I find amazing! There is actually a FAQ entry for Emacs "Why does the Backspace key invoke help?" and a related section from its manual on "If DEL fails to delete". (It might even be the solution you came up with isn't the ideal one, so you might want to read these.)

https://www.gnu.org/software/emacs/manual/html_node/efaq/Bac...

https://www.gnu.org/software/emacs/manual/html_node/emacs/DE...

3 comments

> by essentially subtracting 64 (which is almost but not quite equivalent to masking out a bit)

Subtracting 64 is exactly the same as "masking out a bit". The difference between the control characters and their upper case equivalents is that bit 6 (counting from zero) of the byte is zero for the control characters and is one for the upper case equivalents.

On old terminals, the 'control' key more often than not worked by simply forcing bit six of the character mapped to a given key to zero (i.e., it was a direct hardware change).

Another seemingly little known fact, for ASCII, the difference between upper case and lower case letters is again a single bit. Letter A is 65, letter a is 97, the difference there is bit 5 (counting from zero) is zero for "A" and one for "a".

It isn't the same as "masking out a bit" when you are subtracting 64 from 63 to get 127 (the behavior of C-?). I think it is equivalent to Xor-ing it, but I didn't want to go into that for various reasons, including it just being overly complicated (and, in fact, edited that statement three times in an attempt to find something that was all of true, expository, and easy to understand; but, I apparently didn't optimize for "something everyone would appreciate why it was said the way it was said if they liked adding information but hadn't paid as much as attention to all of the examples as I had been forced to while drafting the comment").
> C-h isn't some crazy invention of readline (...) that you got used to: it is the standard key code for backspace.

> C-H is equivalent to pressing Backspace!! Now, there is also a DEL, for the Delete key, which is which is 127

Not anymore on Linux terminals. People wanted to distinguish backspace and C-h, so in some time in 1990s there was great DEL-BS shift, where convention changed to use 127 for BS and some escape sequence for DEL (in direction from terminal to program, in the opposite direction the original convention is still used). That is why there are these sections in Emacs manual (and similar options on other tools) - to handle situations where terminal behavior and information about terminal are mismatched.

Don't mean to be pedantic, but this comment left me very confused, surely you mean carriage return to be ASCII 13? Also, g is the 7th character and BEL is 7.
Ugh. I've gone ahead and fixed it. I am not having a good night. I was actually sitting here typing all of that because my shoulder hurts so much I can't sleep, and apparently I am so tired I can't count.