Emacs: Change Minor Mode Keys

To change minor mode keys, you need to change the mode's keymap.

Here's a simple example:

( progn ( define-key isearch-mode-map ( kbd "<up>" ) 'isearch-ring-retreat ) ( define-key isearch-mode-map ( kbd "<down>" ) 'isearch-ring-advance ) ( define-key isearch-mode-map ( kbd "<left>" ) 'isearch-repeat-backward) ( define-key isearch-mode-map ( kbd "<right>" ) 'isearch-repeat-forward) )

General Steps

Alt + x describe-function , then type the mode activation command name, then click on the link to open its elisp source code file. in the mode's source code, search for “-map” to find its keymap name. Let's say it's “xyz-map”. in the mode's source code, go all the way to bottom to find (provide 'xyz) if it exists. The “'xyz” would be the symbol for the mode. In your emacs init file, start with (require 'xyz) (if the mode has a symbol), then (define-key keymap_name (kbd " key ") ' command_name )

Not all mode defines a keymap in its source file. Some inherit from other modes, some use keymaps from other elisp file that are not modes, some don't have keymaps.

When you redefine some keys in a mode's keymap, be sure to make keybinding for the displaced commands if you still want a key for them.

For example, in “shell-mode”, i wanted Alt + p to do something. The Alt + p is previous-history-element . I think that command is useful enough to have a hotkey. So, i defined some other key for that. (You can Alt + x describe-key to find out a key's binding while in that mode.)

Examples

Modify Isearch Keys

By default, Ctrl + s will do a interactive search. Pressing that again will find the next match. Suppose you prefer to have Alt + s instead, so in your emacs init file 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 . You want “M-s” to run isearch-repeat-forward . Here's the code to reclaim it:

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

Modify Shell Keys

shell and shell-command 【 Alt + ! 】 have many special keys. Suppose you want to change them. They both use a keymap named comint-mode-map . The following is a example of redefining some of its keys.

( progn ( define-key comint-mode-map ( kbd "M-p" ) ' recenter ) ( define-key comint-mode-map ( kbd "M-n" ) ' nil ) ( define-key comint-mode-map ( kbd "M-r" ) 'kill-word) ( define-key comint-mode-map ( kbd "M-s" ) ' other-window ) ( 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) )

Modify Minibuffer Keys

The minibuffer is where emacs does prompts. It defines the following keys:

C-j exit-minibuffer <RET> exit-minibuffer C-g abort-recursive-edit M-n next-history-element <down> next-history-element M-p previous-history-element <up> previous-history-element M-s next-matching-history-element M-r previous-matching-history-element

Here's a example of how to redefine its keybinding:

( define-key minibuffer-local-map ( kbd "M-p" ) ' recenter ) ( define-key minibuffer-local-map ( kbd "M-n" ) ' nil ) ( define-key minibuffer-local-map ( kbd "M-r" ) 'kill-word) ( define-key minibuffer-local-map ( kbd "M-s" ) ' other-window ) ( 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")