snarfed.org

Ryan Barrett's blog

emacs keybindings with number keys

Wed, 01 Jan 2003 [comments (2)] [history] [rdf] [raw]

gnu.jpg

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.

comment bubble OpenID ashawley, Wed 07 Dec 2005

Use vectors:

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

See "Key Bindings" in the Emacs Manual.

comment bubble OpenID ryan, Fri 06 Jan 2006

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.

Post a comment...



Simple HTML and wiki markup are allowed.