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?

  • 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, 2014 at 16:21

6 Answers 6


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

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


Just found an answer on Super User:

How do you run the previous command in emacs shell?

M-p goes to the previous command.

  • This is much easier and user friendly Apr 15, 2018 at 8:56

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)



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>.


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

  • Icicles doesnt come close to the consistency and support level of helm.
    – RichieHH
    Jan 13, 2020 at 23:23

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
     (require 'em-hist)      
     (use-package eshell-git-prompt
       ;; 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)
  • 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, 2020 at 9:39

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.