4

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.

  • 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
3

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

(shell (current-buffer))
  • 1
    For your init.el: (defun restart-shell () (interactive) (shell (current-buffer))), which can be tied then to a keyboard shortcut. – Droogans Jul 16 '14 at 14:28
1

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.

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.