How do you run the previous command in emacs shell?

47

9

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

Posted 2010-05-11T05:14:38.300

Reputation: 573

Answers

62

M-p does the job

vava

Posted 2010-05-11T05:14:38.300

Reputation: 5 238

Thanks! I searched a bit, but couldn't find it. – Lidmith – 2010-05-11T05:35:36.110

26

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

Posted 2010-05-11T05:14:38.300

Reputation: 421

1It also doesn't seem to work on my Mac; C-up is mapped to a Mac-specific function. – amo – 2014-07-22T18:23:30.870

For me too C-up is mapped to expose. And M-p feels more natural to me. – Indradhanush Gupta – 2014-10-08T09:32:04.670

1Ah, ty. That seems more natural. – Lidmith – 2010-05-12T17:56:19.457

1Even 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 – 2014-06-13T09:01:21.460

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

Posted 2010-05-11T05:14:38.300

Reputation: 1 514

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

DeLorean88

Posted 2010-05-11T05:14:38.300

Reputation: 21

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

Kevin Bartlett

Posted 2010-05-11T05:14:38.300

Reputation: 1

Check your file again. The second define-key should cause a syntax error as the last closing bracket is not matched by a first. – vfclists – 2017-08-26T07:16:00.837