I'm trying to write a function for complete in minibuffer with history.

(Terminal equivalent of "\e[5~": history-search-backward in ~/.inputrc)

With the below function I can get the last command that start with the prefix in the minibuffer. I want to validate this candidate or search the next candidate.

(defun history-search-forward ()
  (setq prefix (minibuffer-contents))
  (loop for elt in extended-command-history
        ;; history starts with word-at-point
        (and (string-match (rx-to-string `(: bos ,prefix) t)
               (delete-region (point) (line-beginning-position))
               (insert elt)
               ;; here a want to launch the command if <Enter>
               ;; or continue looping if <prior>
(define-key minibuffer-local-completion-map (kbd "<prior>") 'history-search-forward)

How can I wait for user input at that point in the loop?

  • 1
    What is your question? – Dan Jul 8 '15 at 13:55
  • Is there a good reason you are trying to write the function yourself? (ido-completing-read "Command: " extended-command-history) will likely do exactly what you want. Also the minibuffer already has completion history. Just use M-n and M-p to scroll through the history. – Jordon Biondo Jul 8 '15 at 14:00
  • If you just want to have better minibuffer completion everywhere, activate ido-mode, and also install and activate ido-ubiquitous, ido-flx, and smex. I also recommend ido-vertical-mode. – Jordon Biondo Jul 8 '15 at 14:04

You can do that with the new generator.el library in Emacs-25 (i.e. you can just stop the computation at that point with yield and go back to it later).

Otherwise, you're going to have to break out of the loop after inserting the element you want, and arrange to keep enough information in buffer-local variables that next time prior is used, your loop can know where to pick up the search. The normal way to keep this info is to store the current position in minibuffer-history-position, this way it'll interact correctly with other history search commands.

Also, I'm not sure in which way your command differs from the predefined previous-complete-history-element.

  • Thanks, I just want previous-complete-history-element.... This function is not map by default. so I will do: (define-key minibuffer-local-completion-map (kbd "<prior>") 'previous-complete-history-element) and thats OK > Thanks a lot Djangoliv – djangoliv Jul 8 '15 at 16:01

If I understood you correctly, you're trying to search through a command's history without returning from the command. This can be done with enable-recursive-minibuffers and an extra completing-read.

In fact, ivy-mode, available as part of the swiper package implements this for every command that uses completing-read. The binding to search through the history is C-r. As you exit your history search with e.g. C-m the history element will be inserted into the minibuffer. Then you can exit once more with C-m to execute the initial command with the history element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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