emacs keybindings with number keys

I use GNU Emacs, and I love it. However, I had trouble recently when I tried to re-bind C-1 to do something new.

Executive summary? Do this:

(global-set-key [(control \1)] 'foo)

Note the escaped 1. Evidently this keystroke syntax interprets numeric arguments as key ids, or something, instead of the key’s actual character. So, to make it interpret the key binding the way we want, we have to escape the 1.

Note that there’s another keystroke syntax, too:

(global-set-key "C-1" 'foo)

I don’t know how to get this to work with number keys…but the first way is good enough for me.

2 thoughts on “emacs keybindings with number keys

  1. Johan Bockgard (from EmacsWiki) describes a similar solution:

    The thing following control can be a symbol or a character. GNU Emacs has no separate character object type and 1 is indistinguishable from C-a as a character.

    (list 1 (type-of 1)) => (1 integer) the number 1/the char ^A (list ?C-a (type-of ?C-a)) => (1 integer) ditto

    (list ‘1 (type-of ‘1)) => (1 symbol) the symbol 1

    ?1 would work too:

    (list ?1 (type-of ?1)) => (49 integer) the number 49/the char 1

    I suggest (global-set-key (kbd “C-1”) ‘foo), btw.

Leave a Reply

Your email address will not be published.