5

I use bind-key and I haven't found anything else that convenient for organizing key bindings. However sometimes I want to bind some commands for more keymaps than one. That's the way I do it now for both maps.

  (bind-keys  :map some-map
               :prefix-map some-prefix-map
               :prefix "PKEY"
               ("KEY1" . command1)
               ("KEY2" . command2))

and I want it to also work this way

  (bind-keys  :map map1 map2
               :prefix-map some-prefix-map
               :prefix "PKEY"
               ("KEY1" . command1)
               ("KEY2" . command2))

EXAMPLE:

  (bind-keys :map org-mode-map emacs-lisp-mode-map
              :prefix-map f14-prefix-map
              :prefix "<f14>"
              ("x" . my-elisp-indent)
              ("y" . paredit-open-round)
              ("z" . paredit-kill))
bertfred
  • 1,699
  • 1
  • 11
  • 23
  • 1
    Do you have a specific example? Sometimes there is a keymap which is common to both modes, e.g. `prog-mode-map` for programming modes. – T. Verron Oct 25 '16 at 09:34
  • I usually define my bindings only in `major-mode` maps. It feels "cleaner" than attaching them to `minor-mode` maps. So I'm not expecting any overlapping. – bertfred Oct 25 '16 at 09:57
  • `prog-mode` is not a minor mode, it's a major mode from which major modes for source-code edition should inherit. Its purpose is to provide hooks which will be executed by all these modes, a keymap from which all these modes will inherit, and so on. And bnding keys in major modes is not cleaner than in minor modes, it's a matter of purpose. – T. Verron Oct 25 '16 at 10:07
  • Again, do you have an example of functions and maps for your question? – T. Verron Oct 25 '16 at 10:10
  • 1
    Indeed I don't think you have a ready-made solution for this usecase. What you do is could create a function defining these bindings and add the function to `org-mode-hook` and `emacs-lisp-mode-hook`. Not a full solution, but less code duplication at least. – T. Verron Oct 25 '16 at 10:57
  • I'm looking for a solution that doesn't involve hooks. – bertfred Oct 25 '16 at 13:38
  • Is there anything more annoying than someone asking you why you need something when it's perfectly obvious. The bind keys could take a list of mode-maps I suspect. It's very common to assign a function binding to disparate modes. – RichieHH Jan 30 '20 at 14:34

2 Answers2

6

You can iterate over the list of modes:

(dolist (m (list org-mode-map emacs-lisp-mode-map))
  (bind-keys :map m
             :prefix-map f14-prefix-map
             :prefix "<f14>"
             ("x" . my-elisp-indent)
             ("y" . paredit-open-round)
             ("z" . paredit-kill)))
db48x
  • 15,741
  • 1
  • 19
  • 23
-1

EDIT: This answer no longer works, with more recent versions of the bind-keys package.

Just put the maps in a list, like so:

(bind-keys :map (org-mode-map emacs-lisp-mode-map)
           :prefix-map f14-prefix-map
           :prefix "<f14>"
           ("x" . my-elisp-indent)
           ("y" . paredit-open-round)
           ("z" . paredit-kill))

One caveat: Last time I checked, the bind-keys handler for use-package doesn't recognize this syntax, so this won't work in a :bind statement inside use-package.

Aaron Harris
  • 2,664
  • 17
  • 22