0

I like Emacs, I like key bindings. I also like the command line and have a lot of key bindings.

Is it possible to dump Emacs key bindings to standard out so that I can search them with things like grep?

Edit: Ideally the output would "play well with command line tools" and be "machine readable". Something like:

"C-x s"  save-buffer     fundamental-mode
"C-a"    beginning-of-line   fundamental-mode 

would be nice.

Edit: As people have pointed out the keybindings in emacs vary depending upon current buffer and position. I would like the "current" bindings, where this is defined as the selected buffer in the last selected frame. These can be accessed programmatically from emacsclient: (emacsclient --eval "(buffer-file-name (window-buffer (selected-window)))" | tr -d '"')

"Clues"

To make clear what I already have thought of, and to give interested readers clues.

The following script dumps functions to standard out in a similar way:

https://pastebin.com/wvPJiUwb

It uses "emacsclient -eval" for the purpose.

I am aware that the describe-bindings function. When I looked at this before the code seemed a little "specific purpose without easy to understand general functions".

I am aware the which-key emacs mode which list key bindings as you type them, so this code might be easy to modify.

This code might be kind of readable.

Alternatives considered

Use emacs self-documentation to find key bindings

I am quite aware of the the ability to search function names and find key bindings associated with functions. The utility is that key bindings represent "functions that I am aware of and have used before".

Also the the prefix keys that I have used for key bindings represent an indexing of some descriptions (these functions are related to such and such)

5
  • 1
    Would something like the following work: emacsclient -eval '(with-temp-file "test" (describe-buffer-bindings (get-buffer "scratch")))' Nov 2, 2017 at 18:21
  • describe-bindings is the only way that I'm aware of to get a list of bindings without the need for having to handle all the arcane details of key-maps yourself.
    – politza
    Nov 2, 2017 at 18:22
  • The part that's unclear to me is which set of key bindings you're wanting to show? The set of keymaps which are active generally vary from buffer to buffer, so you can only establish what "all" the bindings are on a per-buffer basis. That being the case, your command line tool is going to need to specify a buffer -- and you don't seem to have acknowledged that in your question. It would seem more convenient to trigger the behaviour from inside Emacs (where you can just assume the selected buffer is the one of interest), but that doesn't seem to gel with your expectations either.
    – phils
    Nov 2, 2017 at 21:33
  • So I kind of acknowledge this with the word "current" (but rather succinctly). The reason to call this from the shell rather than emacs is because you want to interact with the output from the shell, that lets you do interesting thigns. The concept of the current window and frame still exists outside of emacs, namely the last selected buffer and the last selected shell. I've had quite good mileage having little shell snippets that interact with this: for example $(emacsclient --eval "(buffer-file-name (window-buffer (selected-window)))" | tr -d '"') output the filename (if any) associated
    – Att Righ
    Nov 2, 2017 at 22:09
  • ... with a buffer. I've updated the question to clarirfy this.
    – Att Righ
    Nov 2, 2017 at 22:15

2 Answers 2

1

Your question is not too clear to me, based on what you say is not what you want. Anyway... Perhaps these will help:

  1. If you use library help-fns+.el then you can use C-h M-k to get a listing of all of the keys bound in a given keymap. In other words, it gives you a human-readable view of a keymap.

  2. If you use Icicles then you get key completion, which is a bit like which-key. You can at any time see all of the possible key sequences currently available and what they are bound to, using S-TAB (or M-S-TAB in the minibuffer). This includes menus.

    All-currently-available means also that you can use S-TAB after using a prefix key, to see what keys you can follow it with.

    The available keys and their commands are shown in buffer *Completions*. You can complete minibuffer input against the displayed keys or command names or both. You can do that to narrow the candidates displayed or to finish a key sequence (invoking its command).

    You can navigate the entire forest of key sequences, by changing your minibuffer input. Choosing pseudo-candidate .. moves up a level (e.g. from the display of the keys on prefix C-x r to the display of the keys on prefix C-x).

    You can also see the doc strings of candidates using C-M-RET and other C-M- Icicles candidate-help keys.

    You can also write buffer *Completions* to a file when it is showing key completions. Just use C-<insert> to switch focus to buffer *Completions* and then use C-x C-w (write-file) to write that buffer to a file that you name.

5
  • 1
    question is not too clear... In the second sentence I say "Is it possible to dump emacs keybindings to standard out so that I can search them with things like grep?" the title of the question is "dump list of current keybindings to standard out". So I think I want to dump a list of keybindings to standard out ! The rest is just context and muttering about possible approaches.
    – Att Righ
    Nov 2, 2017 at 18:38
  • The real thing is I want the format to be machine readable and to interact reasonably with command line tools... a flattened csv would be ideal. I guess. I shall update the question to this effect.
    – Att Righ
    Nov 2, 2017 at 18:42
  • You can navigate the entire forest of key sequences, by changing your minibuffer input. Choosing pseudo-candidate .. moves up a level that is very nice indeed. Can I use this without using icicles everwhere? I use helm mostly.
    – Att Righ
    Nov 2, 2017 at 22:56
  • So digging around a little the relevant function seems to be icicle-complete-keys (the code for this is here: github.com/emacsmirror/icicles/blob/master/…). This might be a good place to go looting. I tried calling this function directly without enabling icicles and it did something... but didn't seem very useable. Perhaps I can enabling icicles complete-read patching magically for one call.
    – Att Righ
    Nov 2, 2017 at 23:30
  • helm-descbinds seems like another "ide-esqu" interface to keybindings that may provide more functionality that the build in introspection functions (hence alleviating the need to use the shell for interacting with this sort of stuff)
    – Att Righ
    Nov 2, 2017 at 23:37
0

Well I seem to have hacked up my own solution. Code available here:

https://gist.github.com/talwrii/8c1078993cbfbdfbcd8c0385366472c8

This kind of represents "throwing paint at the wall". I imagine I will incrementally improve this as and if I use this in anger. One imagines json output would be quite useful. Anyone considering using this code might like to consider the more IDE-like powerful interface to keybindings provided by icicles (as suggested in another answer by Drew). I was unable to make this work together with helm but only applied 10 minutes or so of work.

This approach is based on code in which-keys.el and works mapping over keys using map-keymap and current-active-maps

Your Answer

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

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