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.
Asked
Active
Viewed 1,590 times
2 Answers
5
Instead of M-x shell, run the following emacs lisp code using M-:
(shell (current-buffer))
David Nehme
- 235
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.
Fran
- 5,511