4

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))
  • 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 9: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 9: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
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)))
0

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.

  • The docu specifies that :map should be a single map and NOT a list of maps. This doesn't work for me. – mihai Nov 1 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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