0

Suppose I'd like to add a custom key bind to a certain mode map.

I don't want to override an existing key accidently.
How can I find out which keys are free to use for a mode-map?

The best way I can think of is to actually open a buffer with the mode-map and use C-k and try different keys to see if it's avaialle

5
  • 1
    C-h b aka M-x describe-bindings? See also Drew's answer for a library containing describe-keymap: stackoverflow.com/a/7135736/2112489
    – lawlist
    Commented Aug 13, 2023 at 5:42
  • 1
    In the manual, you find the following advice about customizing key-bindings: "Key sequences that consist of C-c followed by a letter (upper or lower case; ASCII or non-ASCII) are reserved for users. Emacs itself will never bind those key sequences, and Emacs extensions should avoid binding them. In other words, users can bind key sequences like C-c a or C-c ç and rely on these never being shadowed by other Emacs bindings."
    – Tobias
    Commented Aug 13, 2023 at 6:28
  • @lawlist I had C-h b bound to helm-descbinds and it was broken (package not downloaded) , I downloaded it and tried it. good ! thanks
    – eugene
    Commented Aug 13, 2023 at 6:40
  • @Tobias oh I didn't understand that sentence back when I read it, thanks for the reminder.
    – eugene
    Commented Aug 13, 2023 at 6:40
  • @Tobias I guess it's a good practice to use C-c [:alpha] as a prefix for a group of commands. Only C-c is special right? not C-x or anything..
    – eugene
    Commented Aug 13, 2023 at 6:43

1 Answer 1

1

Firstly, see C-hig (elisp)Key Binding Conventions

How can I find out which keys are free to use for a mode-map?

Since Emacs 28.1, the standard command M-x describe-keymap shows the keybindings for the specified keymap.

You can usually get this same information from C-hm (or M-x describe-mode) although that isn't guaranteed (it depends on the docstring containing some particular syntax).

The best way I can think of is to actually open a buffer with the mode-map and use C-k and try different keys to see if it's avaialle

It sounds like you're not really interested in a single mode map, but rather in knowing whether the key sequence in question is bound in any active keymap in the buffer?

C-hb (or M-x describe-bindings) will show you all active bindings in the current buffer.

You can also use the key-binding function:

(key-binding KEY &optional ACCEPT-DEFAULT NO-REMAP POSITION)
 
Return the binding for command KEY in current keymaps.
KEY is a string or vector, a sequence of keystrokes.
The binding is probably a symbol with a function definition.

See C-hf key-binding for more.

3
  • Plus a couple of older Q&As of note: emacs.stackexchange.com/questions/59483 and (for Emacs versions older than 28.1) stackoverflow.com/questions/3480173
    – phils
    Commented Aug 13, 2023 at 13:03
  • Why do you say that stackoverflow.com/questions/3480173 is for Emacs versions older than 28.1? It and the answers seem to apply still, AFAICT.
    – Drew
    Commented Aug 13, 2023 at 16:05
  • Just because 28.1 added a describe-keymap command. (I'm sure the code in that answer does still work on later versions.)
    – phils
    Commented Aug 14, 2023 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.