I have the following function that sets keymaps for outline-minor-mode. I wonder how I may avoid repeating all this for different major-modes. Perhaps have a list of modes that I could loop through, but do not know what a good would look like.

(defun tika-elisp-view ()
  "Viewing emacs lisp outline structures."

  (define-key emacs-lisp-mode-map
     (kbd "H-o s") #'outline-show-subtree)

  (define-key emacs-lisp-mode-map
     (kbd "H-o i") #'outline-show-children)

  (define-key emacs-lisp-mode-map
     (kbd "H-o k") #'outline-show-branches)

  (define-key emacs-lisp-mode-map
     (kbd "H-o e") #'outline-show-entry) )

1 Answer 1


I guess you want to provide a list of major modes to some function and have that function define the keys as you show in the keymaps of those modes.

(defun set-my-outline-mode-keys (modes)
  "Set my keys in MODES."
  (dolist (mode  modes)
    (let ((map  (symbol-value (intern (format "%s-map" mode)))))
      (define-key map (kbd "H-o s") #'outline-show-subtree)     
      (define-key map (kbd "H-o i") #'outline-show-children)     
      (define-key map (kbd "H-o k") #'outline-show-branches)    
      (define-key map (kbd "H-o e") #'outline-show-entry))))

(set-my-outline-mode-keys '(emacs-lisp-mode text-mode))

(In the upcoming Emacs 29 release you can use keymap-set instead of define-key. I don't see a great advantage, but you might prefer it.)

  • Yes, providing a list of major modes would get me a much cleaner and compact implementation. Then simply join a -map followed by an intern. Works good.
    – Dilna
    Jun 24, 2023 at 14:36
  • What type of advantages would keymap-set offer in this situation ?
    – Dilna
    Jun 24, 2023 at 14:40
  • No idea. I'm not following that change. Well, it lets you use the same key descriptions as input, directly, without needing to use kbd. Dunno what else is touted.
    – Drew
    Jun 25, 2023 at 1:19
  • Not much difference then, just a different calling sequence.
    – Dilna
    Jun 25, 2023 at 9:59
  • Check NEWS (C-h n) in an Emacs build that has this.
    – Drew
    Jun 25, 2023 at 15:03

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.