2009-10-02

finding the syntax to define shortcuts in emacs

On Oct 1, 12:06 pm, quodlibetor wrote:
> I would really like to bind C-).
>
> I've tried: (all of the following with both local- and global- set
> key)
>
> (local-set-key "\C-)" 'foo) ;error, invalid modifier in string
> "\C-\S-0" ;same
> [C-)] ;unbalanced parenthesis.
> [C-S-0] ; no error, but doesn't work
> [C-(kbd (self-insert-command ")")] ;totally borked
> [C-(kbd ")")] ; doesn't error, but really weird result
>
> etc, i can't remember.
>
> I tried entering into edmacro-mode to figure it out, and it hasn't
> helped me.
>
> Any tips seriously welcome.


The issue about emacs's syntax for defining shortcuts is a frequently asked confusion. Part of it has to do with emacs's keybinding syntax's and emacs lisp's historical baggage.

If you just want a working syntax, the solution is quite symbol.

Press Ctrl+h k (calling describe-key), then type what you want. For example, if you type “Ctrl+Shift+0” (which is “Ctrl+)” in us keyboard layout), then you'll get this message:

C-) is undefined

which means, the correct syntax is: “(kbd "C-)")”.

This tip is given at

• How to Define Keyboard Shortcuts in Emacs
http://xahlee.org/emacs/keyboard_shortcuts.html

---------------------------------

A separate issue, is that there's inconsistency for the kbd function's syntax when the key combo involves Shift.

Here's a except from the above article:

****************
Note: A keypress-combination such as “Meta+Shift+2” can also be considered as “Meta+@”. So, in emacs, you might be thinking that both of these code: “(kbd "M-S-2")” and “(kbd "M-@")” will work. Actually, only the latter will work.

When writing a keybinding definition, for a key combination that involves “Meta+Shift+‹key›”, you must use a version without the shift key if possible. For keybinding involving “Ctrl+Shift+‹letter›”, you must use the "-S". Examples:
GOOD BAD Keystroke
(kbd "M-A") (kbd "M-S-a") Meta+Shift+a
(kbd "M-@") (kbd "M-S-2") Meta+Shift+2
(kbd "M-:") (kbd "M-S-;") Meta+Shift+;
(kbd "C-S-a") (kbd "C-A") Ctrl+Shift+a

A easy way to find out the proper syntax, is to call “Alt+x describe-key”, then type the keystroke.

Note also, that keys involving “Ctrl+Shift+‹key›” cannot be distinguished from “Ctrl+‹key›” when emacs runs in a text terminal (i.e. telnet/ssh). So, this means best to avoid any binding with both Control and Shift.
****************

I filed a bug report on this, but i think it didn't come to any result.

http://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=1111

(i'm pretty sure this particular inconsistency is in part of the thread bug#1111... but the website shows lots of spam now. Possibly it is in another bug report but don't have time to hunt it down at the moment. In particular, i remember Chong Yidong closed it or put it on wishlist or something.)

For more notes related to keyboard shortcut notations, see:

• The Confusion of Emacs's Keystroke Representation
http://xahlee.org/emacs/keystroke_rep.html

• Emacs's M-‹key› Notation vs Alt+‹key› Notation
http://xahlee.org/emacs/modernization_meta_key.html

• A Short Survey Of Keyboard Shortcut Notation
http://xahlee.org/emacs/modernization_hotkey_notation.html

Xah
∑ http://xahlee.org/

No comments:

Post a Comment