If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

How To Reclaim Keybindings In Emacs

Xah Lee, 2008-07

This page shows you how to reclaim keybindings when some major or minor mode override your global keybindings.

When you have made some personal keyboard shortcuts in emacs using global-set-key, major modes will override those if it uses the same keybindings. To reclaim your binding, you'll need to find out the value of the variable “major-mode”, then use a hook to rebind keys in its keymap. Here's a example.

Suppose you defined:

(global-set-key (kbd "M-s") 'isearch-forward)
(global-set-key (kbd "M-S") 'isearch-backward)

You want to use M-s to repeat the search. However, once you are in the isearch prompt, technically it is a minor mode called isearch-mode. In isearch-mode, C-s is defined to run isearch-repeat-forward and M-s is disabled. You want M-s to run isearch-repeat-forward. Here's the code to reclaim it:

(add-hook 'isearch-mode-hook
 (lambda ()
 (define-key isearch-mode-map (kbd "M-s") 'isearch-repeat-forward)
 (define-key isearch-mode-map (kbd "M-S") 'isearch-repeat-backward)
 )
)

In general, when you want to reclaim some bindings used in some mode, first find out the value of the variable “major-mode” while in that mode. Suppose the value is the string “"xyz-mode"”, then the hook name would be the symbol “xyz-mode-hook”, and its keymap name would typically the symbol “xyz-mode-map”.

When you redefine some keys in a mode's keymap, be sure to make bindings for the displaced commands if those commands are important to you. (use describe-key to find out a key's binding while in that mode.)

How To Find The Major Mode's Name And Its Keymap Name

You can find out the major mode's name by switching to the mode, then type “Alt+x describe-variable”, then give “major-mode”, then emacs will show its value.

To find out its keymap name, type “Alt+x describe-function”, then give the major mode's name, then emacs will open a pane showing a link to the source file where the mode is defined. Move cursor to the source file name (underlined), press Enter to open the source file. Then, search for the word “-map”. (If you do not find any “-map” in the source code, then check if it is derived from other major mode or it loads other major mode.)

Note: There are 2 names related to a major mode. One is stored in the buffer local variable named “major-mode” and the other is stored in buffer local variable named “mode-name”. You want to use value of “major-mode”. The “mode-name” variable is used for display purposes only. For example, in html mode, the value of “major-mode” is “html-mode”, and the value of “mode-name” is just “HTML”. Note: the value of the variable “major-mode” does not necessarily end in “-mode”.

Examples Of Reclaim Keybindings

Reclaim Bindings for Minibuffer

The minibuffer is where emacs does prompts. It is technically a minor mode. It defines the following keybindings:

Minibuffer's Keybindings
KeyCommand
C-jexit-minibuffer
Enterexit-minibuffer
C-gabort-recursive-edit
M-nnext-history-element
next-history-element
M-pprevious-history-element
previous-history-element
M-snext-matching-history-element
M-rprevious-matching-history-element

Here's a example how to redefine its keybindings:

;; reclaim some bindings used in minibuffer for ergoemacs bindings for qwerty
(define-key minibuffer-local-map (kbd "M-p") 'recenter) ; was previous-history-element. Use ↑ key or f11.
(define-key minibuffer-local-map (kbd "M-n") 'nil) ; was next-history-element. Use ↓ key or f12.
(define-key minibuffer-local-map (kbd "M-r") 'kill-word) ; was previous-matching-history-element.
(define-key minibuffer-local-map (kbd "M-s") 'other-window) ; was nest-matching-history-element

;; add back some bindings for commands whose binding we displaced
(define-key minibuffer-local-map (kbd "<f11>") 'previous-history-element)
(define-key minibuffer-local-map (kbd "<f12>") 'next-history-element)
(define-key minibuffer-local-map (kbd "S-<f11>") 'previous-matching-history-element)
(define-key minibuffer-local-map (kbd "S-<f12>") 'next-matching-history-element)

(info "(elisp) Text from Minibuffer")

Reclaim Bindings for Shell

The shell mode (Alt+x shell), and shell-command (Alt+!) both have M-‹key› that conflics with our Ergoemacs Keybindings . Here's a example of how to reclaim it.

;; reclaim some binding used by shell mode and shell-command.
;; the shell mode and associated mode and commands use keys in comint-mode-map.
(add-hook 'comint-mode-hook
 (lambda ()
   (define-key comint-mode-map (kbd "M-p") 'recenter) ; was comint-previous-input. Use Ctrl+↑ or f11
   (define-key comint-mode-map (kbd "M-n") 'nil) ; was comint-next-input. Use Ctrl+↓ or f12
   (define-key comint-mode-map (kbd "M-r") 'kill-word) ; was comint-previous-matching-input.
   (define-key comint-mode-map (kbd "M-s") 'other-window) ; was comint-next-matching-input.

   (define-key comint-mode-map (kbd "<f11>") 'comint-previous-input)
   (define-key comint-mode-map (kbd "<f12>") 'comint-next-input)
   (define-key comint-mode-map (kbd "S-<f11>") 'comint-previous-matching-input)
   (define-key comint-mode-map (kbd "S-<f12>") 'comint-next-matching-input)
))
2008-07
© 2008 by Xah Lee.