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 mode's name, 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 mode named isearch-mode. In isearch-mode, C-s is defined to run isearch-repeat-forward and M-s is not defined. 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 mode's elisp name (stored in the local variable “major-mode” in that mode), then the hook name would be xyz-mode-hook, and its keymap name would typically be 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 Keymap Name

Usually the major mode's name and its keymap name is obvious, but sometimes it is not what you expected. Here's how to find them exactly.

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”.

Example: 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 ergo dvorak
(define-key minibuffer-local-map (kbd "M-p") 'kill-word)
(define-key minibuffer-local-map (kbd "M-n") 'forward-char)
(define-key minibuffer-local-map (kbd "M-r") 'forward-word)
(define-key minibuffer-local-map (kbd "M-s") 'isearch-forward)
(define-key minibuffer-local-map (kbd "<f11>") 'previous-matching-input)
(define-key minibuffer-local-map (kbd "<f12>") 'next-matching-input)

(info "(elisp)Text from Minibuffer")

Reclaim Bindings for Shell

The shell mode (M-x shell), and shell command (M-!) both have M-‹key› that conflics with our ergo keymaps. Here's how we fixed it for the ergo keymap dvorak.

;; 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") 'kill-word)
   (define-key comint-mode-map (kbd "M-n") 'forward-char)
   (define-key comint-mode-map (kbd "M-r") 'forward-word)
   (define-key comint-mode-map (kbd "<f11>") 'comint-previous-matching-input)
   (define-key comint-mode-map (kbd "<f12>") 'comint-next-matching-input)
))
2008-07
© 2008 by Xah Lee.