Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In emacs, I often have multiple shell buffers open and use rename-buffer to keep track of them. Sometimes I inadvertently close the shell by hitting too many ^D's, or I'm on a system that automatically times-out inactive shells. Is there a way to start a new shell in the same buffer? Because I have renamed the buffer from shell typing M-x shell will start a shell in a new shell buffer, not the current buffer.

share|improve this question
    
Define "doesn't work". –  Fran May 2 '12 at 17:47
    
M-x shell will simply go to a buffer called shell if it exists or create a new shell buffer if not. I'm trying to restart a shell in the current buffer. –  David Nehme May 2 '12 at 18:44
add comment

2 Answers

This isn't perfect, but it gets you most of what you want. Put this code in your .emacs startup file:

(defun my-shell-mode-hook ()
  "Added to shell-mode-hook.  This function runs every time function shell
starts a shell."
  (rename-buffer "My Shell" 'unique))

(add-hook 'shell-mode-hook 'my-shell-mode-hook)

Feel free to change the string "My Shell" to some other name. If you do M-x shell RET while in the My Shell buffer, then the new shell starts in the same buffer. Unfortunately, if you do it in another buffer, then you get a new buffer named My Shell<2>, which isn't so pretty. I think the latter effect can be eliminated by advising function shell, so that it switches to buffer My Shell before actually running the real shell function.

share|improve this answer
add comment
up vote 0 down vote accepted

Instead of M-x shell, run the following emacs lisp code using M-:

(shell (current-buffer))
share|improve this answer
add comment

Your Answer

 
discard

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.