Here is a generic way to set buffer-specific key bindings.
- Create a temporary minor mode (save the below snippet to
temp-mode.el
and require
it in your init.el
.
- Enable that
temp-mode
minor mode and define that minor-mode's keymap only in the buffer(s) you need.
Put the below Local Variables
snippet in the buffer where you want the custom key binding. Below are examples of org-mode
files.
In buffer ONE
# Local Variables:
# eval: (temp-mode 1)
# eval: (define-key temp-mode-map (kbd "<f10>") 'function-ONE)
# End:
If some other buffer redefines the same key-binding using,
In buffer TWO
# Local Variables:
# eval: (temp-mode 1)
# eval: (define-key temp-mode-map (kbd "<f10>") 'function-TWO)
# End:
then the new binding become effective on doing M-x revert-buffer
.
This is what I would do if I need to switch between these two buffers very frequently and if I have to use the F10 binding in both:
- Work in buffer ONE,
C-x C-s
(save) and switch to buffer TWO
revert-buffer
(refresh bindings), work in buffer TWO, C-x C-s
and switch to buffer ONE
revert-buffer
(refresh bindings), work in buffer ONE, C-x C-s
and switch to buffer TWO
But I would rather bind the different buffer-specific commands to different keys.
Temporary minor mode
;; temp-mode.el
;; Temporary minor mode
;; Main use is to enable it only in specific buffers to achieve the goal of
;; buffer-specific keymaps
(defvar temp-mode-map (make-sparse-keymap)
"Keymap while temp-mode is active.")
;;;###autoload
(define-minor-mode temp-mode
"A temporary minor mode to be activated only specific to a buffer."
nil
:lighter " Temp"
temp-mode-map)
(provide 'temp-mode)
local-unset-key
hooked withorg-mode-hook
might work if that is executed before theLocal Variables:
section is.Local Variables: