Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am in terminal mode on Ubuntu, and I'm running emacs with 2 buffers open, one is a ruby file, and the other is a shell (opened by typing M-x shell ), and when I switch to the shell buffer, I want to run the same command that I ran before. I would normally just hit the up arrow in a terminal window, but in emacs, it simply puts the cursor up one line.

Does anyone know of keystroke to run the previous shell command from within an emacs shell?

share|improve this question
up vote 44 down vote accepted

M-p does the job

share|improve this answer
Thanks! I searched a bit, but couldn't find it. – Lidmith May 11 '10 at 5:35
Thanks @vava.!! – bgs Mar 19 '13 at 14:27

In addition to M-p, you can also use C-up, which I find preferable. The complementary keys M-n or C-down will get you the next command in history.

share|improve this answer
Ah, ty. That seems more natural. – Lidmith May 12 '10 at 17:56
Even though it was not put in the initial question, I have to admit that [C-up] and [C-down] do not work in Emacs via terminal (PuTTY). – avp Jun 13 '14 at 9:01
It also doesn't seem to work on my Mac; C-up is mapped to a Mac-specific function. – amo Jul 22 '14 at 18:23
For me too C-up is mapped to expose. And M-p feels more natural to me. – Indradhanush Gupta Oct 8 '14 at 9:32

You might also add this to your emacs init file:

(define-key comint-mode-map (kbd "<up>") 'comint-previous-input)
(define-key comint-mode-map (kbd "<down>") 'comint-next-input)
share|improve this answer

thiagowfx solution is preferable to me, since I usually try to avoid context-dependency. However, in order for it to work I had to add loading comint mode first:

(progn(require 'comint)
(define-key comint-mode-map (kbd "<up>") 'comint-previous-input)
(define-key comint-mode-map (kbd "<down>") 'comint-next-input))
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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