user619882
user619882

Reputation: 349

Major mode keybindings in Emacs 29

I am a long time evil-mode Spacemacs user, but now would like to roll my own config that copies my favourite parts of Spacemacs. I'm having some trouble with Keybindings for major modes.

I'm using Emacs 29. I've not needed general.el so far. Following this advice for using the newer Emacs 29 keybindings - here. I've had success using keymap-set and which-key-add-keymap-based-replacements. Here's part of my config for the File keybindings I've configured, which works as expected:

(defvar-keymap my-files-keymap
  :doc "Key map for file functions"
  "f" #'find-file
  "s" #'save-buffer)

;;Add new key map to the leader
(keymap-set my-leader-map "f" my-files-keymap)

;;Add labels to the Files keymaps
(which-key-add-keymap-based-replacements my-files-keymap
  "f" "find"
  "s" "save")

;;Add Files to the Leader keymaps
(which-key-add-keymap-based-replacements my-leader-map
  "f" "Files")

But what I don't understand is the idiomatic way to add keybindings for major modes. For example, when in org mode I'd like SPC, m to show the major mode keybindings that I want for org mode, with which-key. But when in a different major mode, such as Clojure mode, I'd like SPC, m to show just the keybindings relevant to Clojure.

Is there any examples using defvar-keymap, keymap-set, and which-key-add-keymap-based-replacements for defining keys for different major modes, idiomatically?

Upvotes: 0

Views: 285

Answers (2)

myname
myname

Reputation: 315

But what I don't understand is the idiomatic way to add keybindings for major modes.

The idiomatic way in Emacs 29+ is to use keymap-set as described in the manual. For example:

(keymap-set emacs-lisp-mode-map "C-c C-f" (lambda () (interactive) (message "Foo")))

Prior to that, the idiomatic and still a valid way was to use define-key:

(define-key emacs-lisp-mode-map (kbd "C-c f") (lambda () (interactive) (message "Bar")))

Explanation:

It is slightly wrong to think about adding keys to modes. We are not adding keybindings to modes, neither major nor minor, we are always adding keybindings to keymaps. Keymaps are associated with modes, by a simple mechanism, but we don't need to dive into that here.

Typically, we have to find out what is the name of a keymap used in a mode for which you would like to alter bindings. There is no difference for it being a minor or a major mode. The convention is to use mode-name-map, but there are both major and minor that do not obey this convention. Most of both major and minor modes do however.

Easy way to check if a mode follows this is to just type M-: mode-name- and than press TAB for completion. You can also check in which keymap is a known key bound by simply asking which function is bound to that key: C-h k and pressing your key. Emacs will answer with the "(found in map)", amongst function name, and some other details. Unfortunately C-h b (describe-bindings) and C-h m (describe-mode), do not display names of keymaps. It would be handy if they did. Perhaps an idea for an easy to make patch for Emacs, for someone interested?

Upvotes: 0

phils
phils

Reputation: 73344

It turns out this wasn't helpful, but I'll leave the answer in case it prevents anyone else writing an equivalent one.


when in org mode I'd like SPC, m to show the major mode keybindings that I want for org mode. But when in a different major mode, such as Clojure mode, I'd like SPC, m to show just the keybindings relevant to Clojure.

I think the nearest thing is:

(describe-keymap (current-local-map))

The local keymap isn't necessarily the major mode keymap; but in practice that will almost always be the case.

But typically you would simply use describe-mode -- C-hm by default -- which in most cases will show you that same information, and more besides.

Upvotes: 0

Related Questions