This page gives a explanation of the several confusing
represenation of non-printable characters such as
\n \r \t \f and
^J ^M ^I ^L and
C-m RET <return> Enter ^m control-m 13 (?\C-m), and the various key syntax for
them in keyboarding related software (X11 modmap, emacs, OS X DefaultKeyBinding.dict, AutoHotkey, …), and how to type non-printable characters in emacs.
On , Will 〔schimpan…@gmx.de〕 wrote:
how can I find the an overview on how to enter meta-characters (⁖ esc, return, linefeed, tab, …) (a) in a regular buffer (b) in the minibuffer when using standard search/replace-functions (c) in the minibuffer when using search/replace-functions using regular expressions (d) in the .emacs file when defining keybindings As far as I can see in all those situations entering meta-characters is addressed in a different way which I find confusing, ⁖ (a) <key> _or_ C-q <key> (b) C-q C-[, C-q C-m, C-q C-j, C-q C-i (c) \e, \r, \n, \t (d) (define-key [(meta c) (control c) (tab c)] “This is confusing!”) Furthermore, they are displayed in a different way,⁖ - actual, visible layout - ^E, ^M, ^L, ^I - Octals I would be happy about pages summarizing such information. Any references available?
The issues involve non-printable chars, its representation, its input method, its input method representation, suppression of a key's normal function, and program language's need to represent non-printables in strings.
Here's a short summary:
quoted-insertcommand lets you type a char and suppress its normal function. For example, if you need to insert a literal Tab or newline char in minibuffer.
(global-set-key [(control b)] 'cmd)and other variations is emacs's syntax to represent keystrokes in elisp. A syntax for key strokes is necessary because keys are not ASCII chars (for example, function keys, Home key). For historical reasons, elisp has several syntaxes to represent the same keystrokes.
The following is a detailed explanation.
Your first item:
The 【Ctrl+q】 (holding the Control key down then type q) is the keyboard shortcut to invoke the command
quoted-insert. After this command is invoked, the key press on your keyboard will force emacs to insert a character represented by that key, and suppress that key's normal function.
For example, if you are doing string replacement, and you want to replace tabs by returns. When emacs prompts you to type a string to replace, you can't just press the Tab ↹ key, because the normal function of a tab key in emacs will try to do a command completion. (and in other Applications, it usually switches you to the next input field) So, here you can do 【Ctrl+q】 first, then press the Tab ↹ key. Similarly, you can't type the Return ↩ key and expect it to insert a newline character, because normally the Return ↩ key will activate the OK button or signal “end of input”.
This input mechanism usually doesn't exist in other text editors. In other text editors, when you want to enter the ASCII Tab character or Carriage Return character in some pop-up dialog, you often use a special representation such as
/r instead. Or, sometimes, by holding down the mouse, then press the key. Or, they simply provide a graphical menu or check box to let you select the special characters. The need to input character literally, is frequently needed in keyboard macro apps. (See: Mac OS X Keyboard Software ◇
Windows Keyboard Software.)
Ctrl+q Ctrl+[, Ctrl+q Ctrl+m, Ctrl+q Ctrl+j, Ctrl+q Ctrl+i
Here, the 【Ctrl+[】, 【Ctrl+m】, 【Ctrl+j】 etc key-press combinations, are methods to input non-printable characters that may not have a corresponding key on the keyboard.
For example, suppose you want to do string replacement, by replacing Carriage Return (ASCII 13) by Line Feed (ASCII 10). Depending what is your operating system and software, usually your keyboard only has a key that corresponds to just one of these characters. But now with the special method to input non-printable characters, you can insert any of the non-printable characters.
When speaking of non-printable characters, implied in the context is some standard character set. Implicitly, we are talking about ASCII, and this applies to emacs. Now, in ASCII, there are about 30 non-printable characters. Each of these is given a standard abbreviation, and several representations for different purposes. For example, ASCII 13 is the “Carriage return” character, with standard abbreviation code CR, and “^M” as its control-key-input representation. (M being the 13th of the English alphabet), and Control-m is the conventional means to input the character, and the conventional method to indicate a control key combination is by using the caret “^” followed by the character.
For the full detail, look at the table in the wikipedia article: ASCII. Here's a excerpt of the non-printable ASCII chars table.
|1||01||SOH||␁||^A||Start of Header|
|2||02||STX||␂||^B||Start of Text|
|3||03||ETX||␃||^C||End of Text|
|4||04||EOT||␄||^D||End of Transmission|
|16||10||DLE||␐||^P||Data Link Escape|
|17||11||DC1||␑||^Q||Device Control 1 (oft. XON)|
|18||12||DC2||␒||^R||Device Control 2|
|19||13||DC3||␓||^S||Device Control 3 (oft. XOFF)|
|20||14||DC4||␔||^T||Device Control 4|
|23||17||ETB||␗||^W||End of Trans. Block|
|25||19||EM||␙||^Y||End of Medium|
In general, the practical issues involved for a non-printable character, in the context of a programing language for text editing, are:
(Note: Emacs has several input methods to enter any non-printable chars in Unicode. See Emacs ＆ Unicode Tips.)
\e, \r, \n, \t
There are good reasons that these are preferred than a literal or the more systematic caret notation. Here are some reasons:
In the above, we discussed non-printable chars:
However, emacs also need a system to represent keystrokes (as used in its keyboard macro system and keybinding).
Keystroke notation is not just a sequence of characters. For example, the F1 key isn't a character. The Alt modifier key, isn't a character nor is it a function in one of ASCII's non-printable character. There's also key combinations (⁖ 【Ctrl+Alt+↑】) and key sequences (⁖ 【F1 f】). The keys on the number keypad, need a different representation than the ones on the main keyboard section.
Emacs's key notation is rather confusing, due to historical reasons from 1980s.
Here are examples of multiple representation for the same keystroke (tested in emacs 22):
; equivalent code for a single keystroke (global-set-key "b" 'beep) (global-set-key  'beep) (global-set-key [?b] 'beep) (global-set-key [(?b)] 'beep) (global-set-key (kbd "b") 'beep) ; equivalent code for a named special key: Enter (global-set-key "\r" 'beep) (global-set-key [?\r] 'beep) (global-set-key  'beep) (global-set-key [(13)] 'beep) (global-set-key [return] 'beep) (global-set-key [?\^M] 'beep) (global-set-key [?\^m] 'beep) (global-set-key [?\C-M] 'beep) (global-set-key [?\C-m] 'beep) (global-set-key [(?\C-m)] 'beep) (global-set-key (kbd "RET") 'beep) (global-set-key (kbd "<return>") 'beep) ; equivalent code for binding 1 mod key + 1 letter key: Meta+b (global-set-key "\M-b" 'beep) (global-set-key [?\M-b] 'beep) (global-set-key [(meta 98)] 'beep) (global-set-key [(meta b)] 'beep) (global-set-key [(meta ?b)] 'beep) (global-set-key (kbd "M-b") 'beep) ; equivalent code for binding 1 mod key + 1 special key: Meta+Enter (global-set-key [M-return] 'beep) (global-set-key [\M-return] 'beep) (global-set-key [(meta return)] 'beep) (global-set-key (kbd "M-<return>") 'beep) ; equivalent code for binding Meta + cap letter key: Meta Shift b (global-set-key (kbd "M-B") 'beep) (global-set-key "\M-\S-b" 'beep) (global-set-key "\S-\M-b" 'beep) (global-set-key "\M-B" 'beep) (global-set-key [?\M-S-b] 'beep) ; invalid-read-syntax (global-set-key [?\M-?\S-b] 'beep) ; invalid-read-syntax (global-set-key [?\M-\S-b] 'beep) ; compile but no effect (global-set-key [?\M-B] 'beep) (global-set-key [\M-B] 'beep) ; compile but no effect (global-set-key [(meta shift b)] 'beep) (global-set-key [(shift meta b)] 'beep) (global-set-key (kbd "M-B") 'beep) (global-set-key (kbd "M-S-b") 'beep) ; compile but no effect ; Meta + shifted symbol key. (global-set-key (kbd "M-@") 'beep) ; good (global-set-key (kbd "M-S-2") 'beep) ; compile but no effect ; to do: show examples of key sequences
Note: keystroke notation is not a new concept. Here are some examples of syntax from different keyboard related software:
One of emacs's quirk is that its character data type are simply integers. So, a character “c” is just the integer 99 in emacs lisp. Now, elisp has a special read syntax for chars, so that the letter “c” in lisp can also be written as
?c instead of
99. This way, it is easier for programers to insert a character data in their program, and easier to read too. A backslash can be added in front of the char, so that
?' can be written as
?\'. This syntax is introduced in part so that Emacs's editing commands don't get confused (because the apostrophe is lisp syntax to quote symbols). Many of the control characters in ASCII also have a backslash representation. Here's a table from the elisp manual:
(info "(elisp) Character Type").
?\a ⇒ 7 ; control-g, C-g ?\b ⇒ 8 ; backspace, <BS>, C-h ?\t ⇒ 9 ; tab, <TAB>, C-i ?\n ⇒ 10 ; newline, C-j ?\v ⇒ 11 ; vertical tab, C-k ?\f ⇒ 12 ; formfeed character, C-l ?\r ⇒ 13 ; carriage return, <RET>, C-m ?\e ⇒ 27 ; escape character, <ESC>, C-[ ?\s ⇒ 32 ; space character, <SPC> ?\\ ⇒ 92 ; backslash character, \ ?\d ⇒ 127 ; delete character, <DEL>
So, the character tab (ASCII 9), can be represented in elisp as a character type data as:
Here's more quote from the manual:
Control characters may be represented using yet another read syntax. This consists of a question mark followed by a backslash, caret, and the corresponding non-control character, in either upper or lower case. For example, both `?\^I' and `?\^i' are valid read syntax for the character C-i, the character whose value is 9.
Instead of the `^', you can use `C-'; thus, `?\C-i' is equivalent to `?\^I' and to `?\^i':
?\^I ⇒ 9 ?\C-I ⇒ 9
… The read syntax for meta characters uses `\M-'. For example, `?\M-A' stands for M-A. You can use `\M-' together with octal character codes (see below), with `\C-', or with any other syntax for a character. Thus, you can write M-A as `?\M-A', or as `?\M-\101'. Likewise, you can write C-M-b as `?\M-\C-b', `?\C-\M-b', or `?\M-\002'.
So now, the tab char can be any of:
9 ?\t ?\^i ?\^I ?\C-i ?\C-I
(info "(elisp) Key Sequences")
Thanks to diszno for a correction on