Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looking for the "best" notation for key-binding

70 views
Skip to first unread message

Chap Harrison

unread,
Sep 20, 2012, 7:11:48 PM9/20/12
to help-gn...@gnu.org
Here are examples of key bindings culled from the Emacs FAQ and emacswiki.  Each one seems to use a slightly different notation to identify the keystroke.


(global-set-key     (quote [f1])   (quote help-for-help))

(global-unset-key   [?\e?{]        )
(global-set-key     [f10]          [?\C-x?\e?\e?\C-a?\C-k?\C-g])
(global-unset-key   "\e["          )
(global-set-key     "\C-h"         'delete-backward-char)
(keyboard-translate ?\C-h          ?\C-?)
(global-set-key     (kbd "C-V")    'somefunction)
(global-set-key     (kbd "<f3>")   'comment-dwim)

It's maddening.  I've so far been unsuccessful in getting this binding to work:

(global-set-key (kbd "C-;") 'comment-indent)

It seems to bind the command to the *unmodified* ';'.

Isn't there a single, simple, consistent way to create key bindings that will always work?

Thanks,
Chap

John Wiegley

unread,
Sep 20, 2012, 8:30:39 PM9/20/12
to help-gn...@gnu.org
>>>>> Chap Harrison <chap.h...@me.com> writes:

> Isn't there a single, simple, consistent way to create key bindings that
> will always work?

I use this:

http://github.com/jwiegley/bind-key

Not only is it completely consistent, but it lets you M-x
describe-personal-keybindings and see what you've changed in your Emacs
environment.

John

B. T. Raven

unread,
Sep 20, 2012, 8:45:41 PM9/20/12
to
Die Thu Sep 20 2012 19:30:39 GMT-0500 (Central Daylight Time) John
Wiegley <jo...@newartisans.com> scripsit:
Try:

(global-set-key [(control \;)] 'comment-indent)


Sorry, John. I first sent this only to you. Something new going on with
Mozilla Tbird.

Ed


Peter Dyballa

unread,
Sep 21, 2012, 5:24:35 AM9/21/12
to c...@pobox.com, help-gn...@gnu.org

Am 21.09.2012 um 01:11 schrieb Chap Harrison:

> Isn't there a single, simple, consistent way to create key bindings that will always work?

I think the vector notation is a good choice:

(global-set-key [C-∫] 'backward-sexp) ; A-C-b
(global-set-key [M-S-return] 'other-window)
(global-set-key [f1 f5] 'apropos-variable)
(global-set-key [f3] 'compare-windows)
(global-set-key [A-f1] 'replace-string)

The commands you bind in your examples the keys are some macros (for f10 for example) or different syntax:

(quote help-for-help) = 'quote help-for-help
(quote [f1]) = [f1]

Entries in this syntax are usually created when using global-set-key interactively and saving the resulting bind commands.

Trying to bind the Lisp comment character ";" to anything can become tricky… In my Emacsen (23.4, 24.2.50) this works:

(global-set-key [67108923] 'comment-indent)

The number value can be found by typing, for example in *scratch* buffer, C-q C-;. This produces a record in the *Messages* buffer you can use.

--
Greetings

Pete

There's no sense in being precise when you don't even know what you're talking about.
– John von Neumann


Stefan Monnier

unread,
Sep 21, 2012, 9:29:21 AM9/21/12
to
> I think the vector notation is a good choice:
> (global-set-key [C-∫] 'backward-sexp) ; A-C-b

This likely won't work. You need

(global-set-key [?\C-∫] 'backward-sexp) ; A-C-b

instead. Yes, it's an annoyance. You have to understand the
distinction between keys that emit characters and other keys (that emit
symbols).

> (global-set-key [M-S-return] 'other-window)
> (global-set-key [f1 f5] 'apropos-variable)
> (global-set-key [f3] 'compare-windows)
> (global-set-key [A-f1] 'replace-string)

These look just fine, yes.

> Trying to bind the Lisp comment character ";" to anything can become tricky…
> In my Emacsen (23.4, 24.2.50) this works:
> (global-set-key [67108923] 'comment-indent)

Now that's very intuitive. A better choice (maybe still not totally
obvious to come across, but at least a bit more obvious to understand
when you read it):

(global-set-key [?\C-\;] 'comment-indent)

> The number value can be found by typing, for example in *scratch* buffer,
> C-q C-;. This produces a record in the *Messages* buffer you can use.

If you type C-x C-e twice in a row, with point right after the magical
number, you'll see alternative ways to write this number, one of them
being the one I used above.


Stefan

Peter Dyballa

unread,
Sep 21, 2012, 10:59:54 AM9/21/12
to Stefan Monnier, help-gn...@gnu.org

Am 21.09.2012 um 15:29 schrieb Stefan Monnier:

>> I think the vector notation is a good choice:
>> (global-set-key [C-∫] 'backward-sexp) ; A-C-b
>
> This likely won't work. You need
>
> (global-set-key [?\C-∫] 'backward-sexp) ; A-C-b
>
> instead. Yes, it's an annoyance. You have to understand the
> distinction between keys that emit characters and other keys (that emit
> symbols).

Yes, it stopped working. A-b produces on my (Mac) keyboard ∫. So ∫ is a symbol just as © or Ω? What makes the distinction? Unicode character classes?

--
Greetings

Pete

When you meet a master swordsman,
show him your sword.
When you meet a man who is not a poet,
do not show him your poem.
– Rinzai, ninth century Zen master


Chap Harrison

unread,
Sep 21, 2012, 11:27:37 AM9/21/12
to Peter Dyballa, help-gn...@gnu.org
On 09/21/2012 03:24 AM, Peter Dyballa wrote:
> Am 21.09.2012 um 01:11 schrieb Chap Harrison:
>
>> Isn't there a single, simple, consistent way to create key bindings that will always work?
> I think the vector notation is a good choice:
>
> (global-set-key [C-∫] 'backward-sexp) ; A-C-b
> (global-set-key [M-S-return] 'other-window)
> (global-set-key [f1 f5] 'apropos-variable)
> (global-set-key [f3] 'compare-windows)
> (global-set-key [A-f1] 'replace-string)

That looks and sounds straightforward enough.

I don't know elisp. Is there an exhaustive list of how to express all
of the key chords using vector notation? For instance, I wouldn't have
guessed that the 'return' key was denoted by 'return' - I usually see it
written RET.

As to my problem with C-; I strongly suspect my binding is getting
stomped by C++ mode. So I'd also appreciate some guidelines on what
keys to use or avoid.

Chap




Stefan Monnier

unread,
Sep 21, 2012, 12:29:06 PM9/21/12
to Peter Dyballa, help-gn...@gnu.org
>>> I think the vector notation is a good choice:
>>> (global-set-key [C-∫] 'backward-sexp) ; A-C-b
>> This likely won't work. You need
>> (global-set-key [?\C-∫] 'backward-sexp) ; A-C-b
>> instead. Yes, it's an annoyance. You have to understand the
>> distinction between keys that emit characters and other keys (that emit
>> symbols).
> Yes, it stopped working.

When did it work?

> So ∫ is a symbol just as © or Ω?

AFAIK they're all characters (my use of `symbol' was in the Lisp sense
of symbol as opposed to integer, string, cons, float, ...).

> What makes the distinction?

The code that turns GUI events into Lisp events, mostly. The general
rule is that keys which should self-insert get turned into
character-events, while other (special) keys get turned into symbol-events.

> Unicode character classes?

Unicode has nothing to do with it, no.


Stefan

Peter Dyballa

unread,
Sep 21, 2012, 4:37:40 PM9/21/12
to Stefan Monnier, help-gn...@gnu.org

Am 21.09.2012 um 18:29 schrieb Stefan Monnier:

>>>> I think the vector notation is a good choice:
>>>> (global-set-key [C-∫] 'backward-sexp) ; A-C-b
>>> This likely won't work. You need
>>> (global-set-key [?\C-∫] 'backward-sexp) ; A-C-b
>>> instead. Yes, it's an annoyance. You have to understand the
>>> distinction between keys that emit characters and other keys (that emit
>>> symbols).
>> Yes, it stopped working.
>
> When did it work?

I think it was in GNU Emacs 22 based "Carbon Emacs".

>
>> So ∫ is a symbol just as © or Ω?
>
> AFAIK they're all characters (my use of `symbol' was in the Lisp sense
> of symbol as opposed to integer, string, cons, float, ...).
>
>> What makes the distinction?
>
> The code that turns GUI events into Lisp events, mostly. The general
> rule is that keys which should self-insert get turned into
> character-events, while other (special) keys get turned into symbol-events.

∫ is a self-insert command:

∫ runs the command self-insert-command, which is an interactive
built-in function in `C source code'.

It is bound to many ordinary text characters.

--
Greetings

Pete

The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."





Jambunathan K

unread,
Oct 1, 2012, 10:31:49 AM10/1/12
to c...@pobox.com, help-gn...@gnu.org
Chap Harrison <chap.h...@me.com> writes:

> Here are examples of key bindings culled from the Emacs FAQ and
> emacswiki. Each one seems to use a slightly different notation to
> identify the keystroke.
>
> (global-set-key (quote [f1]) (quote help-for-help))
> (global-unset-key [?\e?{] )
> (global-set-key [f10] [?\C-x?\e?\e?\C-a?\C-k?\C-g])
> (global-unset-key "\e[" )
> (global-set-key "\C-h" 'delete-backward-char)
> (keyboard-translate ?\C-h ?\C-?)
> (global-set-key (kbd "C-V") 'somefunction)
> (global-set-key (kbd "<f3>") 'comment-dwim)
>
> It's maddening. I've so far been unsuccessful in getting this binding
> to work:
>
> (global-set-key (kbd "C-;") 'comment-indent)
>
> It seems to bind the command to the *unmodified* ';'.
>
> Isn't there a single, simple, consistent way to create key bindings
> that will always work?
>

Use M-x global-set-key RET (or M-x local-set-key RET) and follow the
prompt.

Then M-x list-command-history. You will see the required elisp.

Here is what I get:

,----
| (global-set-key [67108923] (quote comment-indent))
`----

Will above representation work across different platforms or different
invocations of Emacs (terminal/gui/remote). I don't know and I would
like to know.

I can assure you that it will get the job done.

Likewise for local-set-key.

That said, in the long-run, it is better to not meddle with
Emacs-provided bindings.

> Thanks,
> Chap
>
>

--

Kevin Rodgers

unread,
Oct 18, 2012, 11:39:56 PM10/18/12
to help-gn...@gnu.org
On 9/21/12 9:27 AM, Chap Harrison wrote:
> I don't know elisp. Is there an exhaustive list of how to express all of the key
> chords using vector notation? For instance, I wouldn't have guessed that the
> 'return' key was denoted by 'return' - I usually see it written RET.

They are different, see the "(emacs)Named ASCII Chars" info node.

> As to my problem with C-; I strongly suspect my binding is getting stomped by
> C++ mode. So I'd also appreciate some guidelines on what keys to use or avoid.

See the "(elisp)Key Binding Conventions" info node.

--
Kevin Rodgers
Denver, Colorado, USA


0 new messages