3

Using examples from octave mode to explain this, but it would be useful in other places as well.

When going through the command history it would be nice to only display entries that match what's already been typed. For example, if I've typed out x= pressing M-p would go to the last commands beginning with that, eg x=3 or x=y+4, even if several unrelated commands have been entered.

What's the simplest way to achieve this in emacs?

4
  • 1
    If the mode you're using is based on Comint mode, you could use M-x comint-previous-matching-input. (You'll probably want to bind that to some appropriate key.)
    – legoscia
    May 22, 2015 at 19:13
  • Don't M-p and M-n work? I think you should also be able to search the history by pressing M-r and typing the part of the expression you already typed before.
    – wvxvw
    May 22, 2015 at 21:09
  • @wvxvw The M-r shortcut is good, I'll have to remember that one. I'm using M-p (fixed the typo in my question), I just want to add to that behavior.
    – user2699
    May 26, 2015 at 2:46
  • @legoscia, that's exactly what I was looking for. Is there any way to use that in non comint modes, such as the emacs minibufffers?
    – user2699
    May 26, 2015 at 2:48

1 Answer 1

4

You're looking for the commands comint-previous-matching-input-from-input and comint-next-matching-input-from-input, which by default are bound to C-c M-r and C-c M-s.

I recommend binding these command to M-p and M-n, like so:

(eval-after-load 'comint
  '(progn
     ;; originally on C-c M-r and C-c M-s
     (define-key comint-mode-map (kbd "M-p") #'comint-previous-matching-input-from-input)
     (define-key comint-mode-map (kbd "M-n") #'comint-next-matching-input-from-input)
     ;; originally on M-p and M-n
     (define-key comint-mode-map (kbd "C-c M-r") #'comint-previous-input)
     (define-key comint-mode-map (kbd "C-c M-s") #'comint-next-input)))

This should work for any mode the derives from comint-mode.

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.