Emacs global-set-key to C-TAB-Collection of common programming errors

  • Unlike others have suggested, it is a good idea to use kbd (or read-kbd-macro which is basically the same thing) in case you ever want to use the same configuration files in other versions of Emacs; kbd works across several versions of Emacs and XEmacs, where the internal representation of key sequences are different.

    (global-set-key (kbd "") 'my-func)

    The input format used by read-kbd-macro is documented in the docstring of edmacro-mode:

    • The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent special control characters. The words must be written in uppercase.

    • A word in angle brackets, e.g., , , or , represents a function key. (Note that in the standard configuration, the function key and the control key RET are synonymous.) You can use angle brackets on the words RET, SPC, etc., but they are not required there.

    This is written somewhat unfortunately; the TAB referred to in the first bullet point is the ASCII character for TAB, and adding the Control modifier does something nonsensical to it. When you press Control-Tab, Emacs sees it (via your windowing system; it will not work in a text terminal) as with a Control modifier, which you can represent as C- or .

  • It’s because you are using read-kbd-macro incorrectly. When you see what is bound to a key:

    C-h k C-TAB

    Emacs tells you:

     is undefined.

    You need to include the in your invocation of read-kbd-macro.

    (global-set-key (read-kbd-macro "") 'my-func)

    And, I don’t know how to generate , but it’s not the same as .

    (equal (kbd "") (kbd ""))
  • Note that you can also call global-set-key interactively. You can then see the correct binding command with repeat-complex-command (see also KeybindingGuide):

    1. M-x: global-set-key
    2. Type the key combination you want
    3. Use C-x ESC ESC (repeat-complex-command) to see the apropiate command. In your case I get:

      (global-set-key (quote [C-tab]) (quote my-func))
  • Instead of using read-kbd-macro, try using the more plain syntax?

    ;(global-set-key [(control tab)] 'my-func)

    Perhaps the plainer syntax will make a difference?

    More on read-kbd-macro and global-set-key.

Originally posted 2013-11-09 19:44:01.