eshell search history
Asked Answered
R

2

11

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.

Rhnegative answered 22/10, 2012 at 10:52 Comment(0)
D
12

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.

Diactinic answered 22/10, 2012 at 20:46 Comment(2)
is there a way to search back with the text at eshell prompt being only a part of the history as opposed to the starting text?Ierna
@Ierna check my answer to see whether it does you want.Basso
B
1

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)
Basso answered 1/6, 2023 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.