0

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

colorscheme +
source %
shell
set spell
colorscheme?
bp
term

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.

TomRoche
  • 592
  • 3
  • 20
aks
  • 135
  • 5
  • Possible duplicate of https://emacs.stackexchange.com/q/4189/11241 . – Steve Vinoski Dec 07 '19 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 07 '19 at 15:17
  • Does this answer your question? [Access minibuffer history without using arrow keys](https://emacs.stackexchange.com/questions/4189/access-minibuffer-history-without-using-arrow-keys) – Drew Dec 07 '19 at 15:29
  • @aks I couldn't tell from your question whether or not you already knew about minibuffer history navigation, which is why I mentioned the possible duplicate question. I felt that if you already knew about `M-p` and `M-n` then you'd likely also already know about `M-r`, which is a possible answer to your question. – Steve Vinoski Dec 07 '19 at 15:33
  • @SteveVinoski `M-r` requires a regexp. If it try `M-r` with a single letter, it takes me to the last command with that letter *anywhere* in the name. – aks Dec 07 '19 at 15:46
  • 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. – Steve Vinoski Dec 07 '19 at 15:56
  • @SteveVinoski Thank you! That works. – aks Dec 07 '19 at 15:58
  • 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 08 '19 at 10:39

1 Answers1

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.

Steve Vinoski
  • 251
  • 1
  • 4