49

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?

Lidmith
  • 593

5 Answers5

64

M-p does the job

vava
  • 5,948
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.

Prakash K
  • 431
  • 3
  • 4
5

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)
thiagowfx
  • 1,808
2

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))
0

DeLorean88's answer worked for me, but only with a second closing bracket on the "progn" line:

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