3

I use Emacs for few years, learning keybinding from time to time. Often, I forget they after a short time. Now I've started to learn keybindings with Anki. And it raised an interesting question: how many keybindings does Emacs have?

By keybinding I mean full chain that calls specific function. I know that Emacs have a huge number of major and minor mode. So, how one can get full list of bindings in specific mode?

And, by the way, how many keybindings does Org-mode have?

4
  • vimeo.com/77451201#t=78
    – phils
    Commented Oct 4, 2022 at 12:22
  • 1
    Define "Emacs". Emacs can have as many or as few key bindings as you like.
    – Drew
    Commented Oct 4, 2022 at 15:59
  • 1
    How many do you want there to be?
    – davidbak
    Commented Oct 4, 2022 at 22:27
  • You say you've used Emacs a few years? Well, learn only 10 keybindings a day, consistently, and in another few years you'll know them all!
    – davidbak
    Commented Oct 4, 2022 at 22:28

3 Answers 3

4

C-h b shows the list of all keybindings in sections.

In Fundamental mode, the help buffer contains about 1700 lines (C-u M-=). Not all of them are bindings, but it is not very important.

In Org-mode, the help buffer contains more than 2000 lines. But keybindings there are very unusual. As in the documentation, C-c "is a very busy key" which has many functions in different contexts. Another example, C-c down calls org-shiftdown function, the behaviour of which depends on context.

To get the number of keybindings that depend on context is not so simple as it seems.

2

As you know, Emacs's keybindings vary depending on the current major-mode, the active minor-modes, and sometimes even the location of point in the buffer.

"C-h b", describe-bindings will bring up a list of available keybindings in the current buffer. It includes global keybindings, which are not mode-specific. But the bindings are divided into sections for the mode that defines them. In recent Emacs versions, the buffer is presented in outline-minor-mode, so you can fold the different sections in order to ease browsing, a significant improvement given the number of bindings available.

Another way to get specific information about the active modes in a buffer (including the keybindings they provide) is "C-h m", describe-mode. However, the information provided by this command depends on the documentation written for each mode. So it might not be as comprehensive or thorough as describe-bindings.

Since you mention forgetting bindings, you might enjoy the which-key package if you're not already aware of it. It shows available bindings whenever you enter a prefix key and pause before entering the next key in the sequence. So if you press "C-c" and wait a moment, it will show you all the available keybindings that start with that prefix in the current buffer (same for "C-x", "C-h", "M-g", "M-s", etc.). Unlike describe-bindings, you don't have to press any extra keys to see the bindings, and you don't have to browse and manage a separate "*Help*" buffer and window. You simply press the next key in the sequence, and it gets out of your way. It also includes a command to show top-level bindings (those that don't start with a prefix key). There are some other packages that do this, but which-key appears to be the most popular.

I notice the body of your question seems more specific than the title. The title asks how many keys "does Emacs have", and the body seems more about how many bindings are available in a given mode.

But this got me wondering about it myself. I did some playing around with ripgrep in a shell and came up with the following attempt to roughly approximate the total number of keybindings in Emacs:

KeyBindFuncs="global-set-key local-set-key define-key org-defkey"
for func in $KeyBindFuncs; do rg -g *.el "^[^;]*\($func "; done | wc -l

This searches recursively through all elisp files within the current directory tree and counts all lines that appear to call one of the listed keybinding functions. It excludes commented lines, since those wouldn't be evaluated.

Caveats:

  • It only counts uses of the functions global-set-key, local-set-key, define-key, and org-defkey. The latter is what org-mode uses to set most of its keybindings. There are other ways one could set a keybinding, perhaps other specialized commands. It could even be done by directly manipulating a keymap list. But I believe the first three functions in the list are the ones most commonly used.

  • Where those function-calls are repeated, such as when used in a macro that's called several times or in a dolist form that defines a list of keybindings, the occurence of the keybinding function is only counted the one time that it appears in the text, rather than the many times it is actually being called.

  • This count will include bindings to keymaps (prefix-keys). However, the number of prefix keys counted is likely to be smaller than the number of bindings left uncounted due to the reasons listed above.

  • Emacs consists of a lot of packages. Some of them may not be in wide use anymore, but they continue to be included in Emacs because someone somewhere is still relying on them. Those packages--when they're actually used--also create keybindings. But their keybindings are only enabled when they're loaded and a particular keymap is active. So even though Emacs has a lot of keybinding definitions, most of the time only a small fraction of those keybindings are set, and an even smaller fraction are available at a given moment.

    So this is not any sort of indication of how many keybindings an Emacs user needs to memorize. The only keybindings you need to memorize are the ones you actually use.

That said, running the above command lines from the root of the current Emacs master branch returns:

8618

Good grief, that's a lot of keybindings!

If we filter the list to only include lines starting with "lisp/org/" (the subdirectory where org-mode's files go), the result is:

526

I think those are pretty close to the actual number of defined keybindings in the whole of Emacs and in org-mode, respectively.

1

If you use Icicles then S-TAB (by default) at top level shows you all possible key bindings in the current context, regardless of what (modes etc.) might be responsible for those bindings.

It shows the key bindings as completion candidates, in buffer *Completions*, for key completion.

And as always in icicle-mode, the current number of completion candidates is shown in the mode-line of buffer *Completions*. E.g., 394 candidates. So without any filtering, this number is the number of all key bindings available in the current context. This is available any time, in any context (and not just at top-level, but also following prefix keys).

See Icicles - Completions Display for description and screenshot.

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.