Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redefine a key inside a "minibuffer" mode-map?

I'm trying to redefine the keys used to navigate the history when inside several commands accepting regexps and offering C-p / C-n history navigation. I'd like to use other keys, in addition to C-p / C-n. For example when using occur or replace-regexp, C-p and C-n can be used to go to previous and next elements.

I've tried several things but can't make it work. I think I'm missing the "big picture" here.

Which mode-map do I need to modify, when and how? Everything I tried failed.

P.S: Note that I've got my own minor mode with all my keymaps as adviced here.

like image 251
Cedric Martin Avatar asked Nov 03 '13 20:11

Cedric Martin


People also ask

Is it possible to change the keymap of a buffer?

You can change the global keymap, in which case the change is effective in all major modes (except those that have their own overriding local bindings for the same key). Or you can change a local keymap, which affects all buffers using the same major mode.

Why does Emacs use the minibuffer to read CMD?

Emacs keeps reading the key to rebind until it is a complete key (that is, not a prefix key). Thus, if you type C-f for key, that’s the end; it enters the minibuffer immediately to read cmd. But if you type C-x, since that’s a prefix, it reads another character; if that is 4, another prefix character, it reads one more character, and so on.

How do I redefine a key in Emacs?

The way to redefine an Emacs key is to change its entry in a keymap. You can change the global keymap, in which case the change is effective in all major modes (except those that have their own overriding local bindings for the same key). Or you can change a local keymap, which affects all buffers using the same major mode.

What does local-unset-key do in a keymap?

Similarly, local-unset-key makes a key undefined in the current major mode keymap, which makes the global definition (or lack of one) come back into effect in that major mode.


1 Answers

I'm assuming you just needed minibuffer-local-map. Subsequent definitions using keys previously assigned to that key map will trump the prior definitions. To disable a prior key assignment, then just create a new definition and set the last portion to nil instead of 'function-name.

(define-key minibuffer-local-map (kbd "<f6>") 'help-for-help)

Here is an excerpt from Emacs Trunk .../lisp/bindings.el:

(let ((map minibuffer-local-map))
  (define-key map "\en"   'next-history-element)
  (define-key map [next]  'next-history-element)
  (define-key map [down]  'next-history-element)
  (define-key map [XF86Forward] 'next-history-element)
  (define-key map "\ep"   'previous-history-element)
  (define-key map [prior] 'previous-history-element)
  (define-key map [up]    'previous-history-element)
  (define-key map [XF86Back] 'previous-history-element)
  (define-key map "\es"   'next-matching-history-element)
  (define-key map "\er"   'previous-matching-history-element)
  ;; Override the global binding (which calls indent-relative via
  ;; indent-for-tab-command).  The alignment that indent-relative tries to
  ;; do doesn't make much sense here since the prompt messes it up.
  (define-key map "\t"    'self-insert-command)
  (define-key map [C-tab] 'file-cache-minibuffer-complete))
like image 105
lawlist Avatar answered Sep 28 '22 06:09

lawlist