2

Assume that I have defined a function like below with some keybinding (e.g.M-p).

(defun foo () (interactive)
(insert "something very long") )


(global-set-key [(meta p)] 'foo)

If I hit M-p while the cursor is at the main buffer, of course the text something very long is inserted. But when the cursor is at mini-buffer by hitting M-p the text foo is inserted.

Is there a way to redefine this function in such a way to work in both main buffer and mini-buffer?

  • 1
    You might need to bind the key in one of the various minibuffer-XXXX-maps. – Dan Aug 16 '15 at 17:26
  • Unless the string will always vary, it is usually better to send the string directly to the function behind the scenes instead of going through the mini-buffer, which is probably being opened by functions such as read-string and the like. If there is a specific function that you wish to have receive the string, consider editing the question with an example so that a forum participant can demonstrate how to pass the string directly to the function programmatically and bypass the mini-buffer entirely. – lawlist Aug 16 '15 at 17:34
  • @lawlist during evaluation by M-: I need to insert pairs of parentheses and putting the cursor between them. This was my main motivation to use a function which does this automatically. – Name Aug 16 '15 at 19:15
4

The reason it doesn't work is: (a) the minibuffer has its own local keymap, (b) M-p is bound in that local map to a different command, and (c) a local keymap takes precedence over the global keymap.

To have your command work for M-p in the minibuffer, you need to bind it to M-p in one or more of the minibuffer keymaps. It is probably sufficient to bind it in keymap minibuffer-local-map, where it will be inherited by the other minibuffer keymaps:

(define-key minibuffer-local-map (kbd "M-p") 'foo)

See the Elisp manual, node Text from Minibuffer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.