Here's an excerpt from the comment:
I always thought it was a shame the ascii table is rarely shown in columns (or rows) of 32, as it makes a lot of this quite obvious. eg, http://pastebin.com/cdaga5i1 It becomes immediately obvious why, eg, ^[ becomes escape. Or that the alphabet is just 40h + the ordinal position of the letter (or 60h for lower-case). Or that we shift between upper & lower-case with a single bit.
In the terminal you can type these control characters by holding the
CTRL (control characters, get it?) key in combination with another key. For example, as many experienced vim users know pressing
CTRL+[ in the terminal (which is
^[ in caret notation) is the same as pressing the
ESC key. But why is the escape key triggered by the
[ character? Why not another character? This is the insight soneil shares with us.
Remember that ASCII is a 7 bit encoding. Let's say the following:
- The first two bits denote the group of the character (22 so 4 possible values)
- The remaining five bits describe a character (25 so 32 possible values)
In the linked table, which I reproduce below, the four groups are represented by the columns and the rows represent the values.
Now in this table, look for ESC. It's in the first group, fifth from the bottom. It's in the first column so its group has bits '00', the row has bits '11011'. Now look on the same line, what else is there? Yep, the '[' character is there, be it in a different column:
10 11011means [
00 11011means ESC
So when we you type
ESC, you're asking for the equivalent of the character
[) out of the control set. Pressing CTRL simply sets all bits but the last 5 to zero in the character that you typed. You can imagine it as a bitwise AND.
10 11011 ([) & 00 11111 (CTRL) = 00 11011 (ESC)
This is why
^J types a newline,
^H types a backspace and
^I types a tab. This is why if you
cat -A a Windows text file, it has ^M printed all over (meaning
CR, because newlines are
CR+LF on Windows).