11

Of course when I am in Terminal and I press the up arrow key, it goes to the previous command. However, when I hit the M-x keys and type shell an integrated shell comes up in Emacs:

Shell in Emacs

However, if I type a command and press up, this happens:

When I press the up button in Shell Emacs

How do I get back to the last command I typed?

Malabarba
  • 22,878
  • 6
  • 78
  • 163
programking
  • 7,064
  • 9
  • 41
  • 62
  • You can also go up with navigation keys to the line with the command you want to execute and hit `RET`. Of course `C-up` is much easier. – giordano Oct 20 '14 at 16:21

6 Answers6

23

The function that cycles backwards through input history, saving input. is comint-previous-input

It is bound to C-up, M-p.

caisah
  • 4,056
  • 1
  • 23
  • 43
7

Just found an answer on Super User:

How do you run the previous command in emacs shell?

M-p goes to the previous command.

programking
  • 7,064
  • 9
  • 41
  • 62
5

You could use helm-eshell-history for interactive history selection, and replace the original C-c C-l:

(require 'helm-eshell)

(add-hook 'eshell-mode-hook
          #'(lambda ()
              (define-key eshell-mode-map (kbd "M-l")  'helm-eshell-history)))

;; for shell-mode
(define-key shell-mode-map (kbd "C-c C-l") 'helm-comint-input-ring)

helm-eshell-history

Tu Do
  • 6,772
  • 20
  • 39
3

My favorite commands for cycling through shell command history are comint-previous-matching-input-from-input and comint-next-matching-input-from-input. If the prompt is empty, they will behave exactly like comint-previous-input and comint-next-input cycling through all history items. Though if you have entered rake, for example, they will cycle through your shell command history of commands starting with rake.

By default these are bound to C-c M-r and C-c M-s respectively, but I find those not ideal. I'm personally binding them to M-TAB and <M-S-tab>.

waymondo
  • 1,384
  • 11
  • 16
1

Things have changed in Eshell a little.
The module em-hist.el is implemented based on the minor mode eshell-hist-mode since commit 1ee0192b79 in the Emacs git-repro. Currently, this commit is only in the master branch of Emacs (i.e., 28.0 as of today). It has been reverted in the Emacs 27.1 by the commit 478638e470 because of Bug#41370.

So here is my updated working solution for using helm-eshell-history

   (use-package eshell
     :config
     (require 'em-hist)      
     (use-package eshell-git-prompt
       :config
       ;; the powerline prompt is best but I've no idea how to shorten it to the last directory.
       (eshell-git-prompt-use-theme 'powerline)
       (define-advice eshell-git-prompt-powerline-dir (:override () short)
         "Show only last directory."
         (file-name-nondirectory (directory-file-name default-directory))))
     :bind (:map eshell-hist-mode-map
                 ;; ("<down>" . 'next-line)
                 ;; ("<up>" . 'previous-line)
                 ;; ([remap eshell-previous-matching-input-from-input] . helm-eshell-history)
                 ([remap eshell-list-history] . helm-eshell-history)
                 ))
Tobias
  • 32,569
  • 1
  • 34
  • 75
RichieHH
  • 848
  • 4
  • 9
  • Dear RichieHH, I added a reference to the change in the Emacs repro. Hope that is okay for you. Just revert if it isn't. – Tobias Aug 26 '20 at 09:39
1

Another possibility, if you use Icicles: Use C-c TAB (command icicle-comint-command) to choose a previously entered command with completion (or cycling).

Drew
  • 75,699
  • 9
  • 109
  • 225