Assume that I have downloaded a major mode called magical-mode
, and it has its own magical keymap called magical-mode-map
. This mode also provides a hook magical-mode-hook
which is run each time magical-mode
becomes the major mode of a buffer. Now I want to modify my init file to add a few custom key bindings to use in that mode.
It seems that there are (at least) two ways to set up custom key bindings for magical-mode
. The one that I see most commonly is this:
(defun my-magical-keys ()
(local-set-key (kbd "C-i") 'previous-line)
(local-set-key (kbd "C-k") 'next-line)
(local-set-key (kbd "C-j") 'backward-char)
(local-set-key (kbd "C-l") 'forward-char))
(add-hook 'magical-mode-hook 'my-magical-keys)
But it is also possible to do it this way:
(define-key magical-mode-map (kbd "C-i") 'previous-line)
(define-key magical-mode-map (kbd "C-k") 'next-line)
(define-key magical-mode-map (kbd "C-j") 'backward-char)
(define-key magical-mode-map (kbd "C-l") 'forward-char)
The second method actually seems cleaner to me. Are there any advantages to doing it one way over the other?
C-n
andC-p
. The example is just dummy code. I wanted to come up with some very simple example modes and example bindings, precisely so that the bindings themselves would not distract from the actual purpose of the question.add-hook
orrequire
. See here