I am a fiend for C-r
(isearch-backward), and I often will enter the minibuffer with M-x
(execute-extended-command), then use C-r
to search for a previous command. I'd like to bind a key that drops me into the minibuffer to read an extended command and automatically activates isearch-backward, but I'm not sure how to write such a function.
1 Answer
This should help:
(defun my-M-x (arg)
(interactive "P")
(minibuffer-with-setup-hook
(lambda () (call-interactively #'previous-matching-history-element))
(execute-extended-command arg)))
(global-set-key (kbd "M-x") 'my-M-x)
C-r
afterM-x
searches backward in the minibuffer. It does not "search for a previous command". Perhaps you meanM-r
? Or perhaps you have something in your init file that makes Emacs behavior different in this regard? (If so, specify it as part of your question.)emacs -q
,C-r
in theM-x
prompt does search through command history. I am using emacs 24.3 on this computer, so maybe it's something that changed in recent versions?C-r
afterM-x
runs Isearch backward.C-r
, which is backward Isearch fails when searching the minibuffer text backward, then it searches backward through the input history (likewise, forC-s
).M-r
just immediately searches backward through the history.