user2249626
user2249626

Reputation: 453

Emacs global-set-key in find-file

In my init.el file I have this:

(global-set-key "\M-n" (lambda () (interactive) (insert "~")))

This allows me to use Alt-n on my Mac to produce the ~ character. It works fine in buffers etc. but when I try to use it in find-file I get

"End of history no default available".

C-h k reveals that M-n actually calls:

(lambda nil (interactive) (insert "~"))

Why doesn't this work with find-file?

Upvotes: 1

Views: 425

Answers (1)

Drew
Drew

Reputation: 30708

Global key bindings are overridden by local (i.e., major-mode) key bindings, which are overridden by minor-mode bindings, which are overridden by... IOW, there are lots of levels of key binding.

In this case, your global binding is overridden by a minibuffer keymap binding.

In the minibuffer completion keymaps, which are local maps, M-n is bound to next-history-element. If you want M-n in such a map to be bound to something else, then you need to bind it. For example:

(define-key minibuffer-local-completion-map "\M-n" 'your-command)

There are several minibuffer completion keymaps, depending on your Emacs version. The two main ones are minibuffer-local-completion-map and minibuffer-local-must-match-map.

Upvotes: 2

Related Questions