In Vim I could retrieve past commands by typing :<first-letter-of-command>↑ . If my Vim command history is:-

colorscheme +
source %
set spell

Typing :s↑ directly shows me the command set spell.

How can I achieve this in Emacs? I already have the line (savehist-mode 1) in my init.el but pressing M-x <first-letter-of-command>↑ does not work. I am using Emacs 25.3.1 on Windows 10.

Edit: My question is not a duplicate of this suggested question. My question is about accessing past commands based on the first letter of the command. For instance, pressing M - x b followed by the up arrow should take me to the most recent command beginning with the letter 'b'. The suggested question is about going through the command history one-by-one without using the arrow keys.

  • Possible duplicate of emacs.stackexchange.com/q/4189/11241 . Dec 7, 2019 at 15:15
  • @SteveVinoski how is it a duplicate? My question is about accessing past commands using the first Letter of the command
    – aks
    Dec 7, 2019 at 15:17
  • Does this answer your question? Access minibuffer history without using arrow keys
    – Drew
    Dec 7, 2019 at 15:29
  • 1
    @aks Correct; prefix your letter with ^ in your regexp, e.g. ^a, to force the match to be at the beginning of the matched history item. Dec 7, 2019 at 15:56
  • 1
    There are lots of history completion extensions (Helm, Ivy, etc.) which are much more effective (e.g. you can search for words in any order in the history). Don't stop at the built in completion, because there are better ways.
    – Tom
    Dec 8, 2019 at 10:39

1 Answer 1


You can use minibuffer history search to find a matching item via M-r. For example, to find the previous history item beginning with the letter 'a':

M-x M-r ^a Enter

Note that this is a regexp search, so you need to prefix the search letter with the ^ regexp metacharacter to force matches to the beginning of matched history items.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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