Whenever I run M-x shell
, a buffer appears almost randomly somewhere - splitting current windows as it sees fit. I cannot work out the logic behind it.
The docstring says:
(shell &optional BUFFER)
If BUFFER exists but shell process is not running, make new shell.
If BUFFER exists and shell process is running, just switch to BUFFER.
This just tells me it will open a shell if it already exists, otherwise start a new one.
I only care about the position of the buffer. If I have the window configuration set how I need it, running shell usually ruins that setup.
I did look into the source as far as I could, but am not familiar enough with the underlying eLisp.
I would like to have a function that splits the current window, opens a shell buffer - I perform some short operation - then I can close the newly created window, restoring the previous window configuration exactly as it was.
I have this working for eshell
, which I stole from Howard Abrams. It looks like this:
(defun eshell-here ()
"Opens up a new shell in the directory associated with the
current buffer's file. The eshell is renamed to match that
directory to make multiple eshell windows easier."
(interactive)
(let* ((parent (if (buffer-file-name)
(file-name-directory (buffer-file-name))
default-directory))
(height (/ (window-total-height) 3))
(name (car (last (split-string parent "/" t)))))
(split-window-vertically (- height))
(other-window 1)
(eshell "new")
(rename-buffer (concat "*eshell: " name "*"))
(insert (concat "ls"))
(eshell-send-input)))
And the function to close the new buffer:
(defun eshell-close ()
"Closes the window created by the function 'eshell-here'"
(interactive)
(insert "exit")
(eshell-send-input)
(delete-window))
I first tried simply putting replacing eshell
with shell
, but that led to the random buffer positions.
*shell*
, see this identical/related thread: emacs.stackexchange.com/a/28924/2287*eshell*
, see this identical/related thread: emacs.stackexchange.com/a/28730/2287 You can usemy-display-buffer
(from the previous linked thread) witheshell-get-buffer-create
.shell-get-buffer-create
seems to work great for opening a new buffer in a predictable location. Theshell-pop
package also does a great job - like Howard Abramseshell-
functions I posted above.