eshell search history

2.3k Views Asked by At

I'm using emacs eshell and would like to search back through my command history. In bash you would do C-r then type something and then press C-r repeatedly until you find the command you want. On eshell it seems I have to type M-r and then type part of the command and press enter then type M-r and enter again to get the next match and so on. This means I have to keep doing M-r {enter} M-r {enter} M-r {enter} again and again rather than just pressing C-r again and again without moving my hands, is there a better way? There's not much info out there on eshell.

2

There are 2 best solutions below

3
On

If the string that you are searching for is in the beginning of the command, then eshell-previous-matching-input-from-input UP, M-p or C-c M-r is much friendlier than eshell-previous-matching-input M-r.

You can type the first few characters of the command and press UP or M-p key and it will cycle only through the matching commands in the history.

0
On

The key M-p can be used to cycling previous match command history if you type the match pattern on eshell input.

M-p is bound to eshell-previous-matching-input-from-input. But this function matches from start of text. The following will matches any part of the history line:

;; copy most from eshell-previous-matching-input-from-input function.
(defun my-eshell-previous-matching-input-from-input (arg)
  "Search backwards through input history for match for current input.
\(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match.
If N is negative, search forwards for the -Nth following match."
  (interactive "p")
  (if (not (memq last-command '(eshell-previous-matching-input-from-input
                eshell-next-matching-input-from-input)))
      ;; Starting a new search
      (setq eshell-matching-input-from-input-string
        (buffer-substring (save-excursion (eshell-bol) (point))
                  (point))
        eshell-history-index nil))
  (eshell-previous-matching-input
   (regexp-quote eshell-matching-input-from-input-string)
   arg))

;; override eshell-previous-matching-input-from-input, because it limits the search is from the beginning.
(advice-add 'eshell-previous-matching-input-from-input :override #'my-eshell-previous-matching-input-from-input)