I battled with this phenomenon for years and discovered that Emacs automatically recreates a scratch buffer if there are no other buffers. In addition, Emacs creates a scratch buffer on startup. There are a variety of factors that affect how buffers are displayed on startup, including, but not limited to the hard coded (baked-in) startup.el
. The startup.el
file cannot be changed without rebuilding Emacs subsequent thereto so that those changes can be incorporated into the build itself. People who use the popular desktop.el
library to restore a previous layout are necessarily using the after-init-hook
to restore the prior session -- so that can affect what buffer is displayed when Emacs starts. I would suggest using the emacs-startup-hook
, which runs later than the after-init-hook
, to either bury the scratch buffer or kill it. Another idea that I just thought of, but haven't tested, is to set the initial-major-mode
variable to the value of a custom function that deals with how you wish to handle that buffer -- but the O.P. may need to experiment with when that setting gets taken into consideration during startup. There are other factors to consider such as newer versions of Emacs add support for an early-init.el
file where settings can be stored. Here is an example utilizing the emacs-startup-hook
:
(add-hook 'emacs-startup-hook (lambda ()
(when (get-buffer-window "*scratch*")
(bury-buffer "*scratch*"))))
Feel free to play around with that example and customize it to suit your needs; e.g., (kill-buffer "*scratch*")
instead of burying it, then put your preferred buffer in its place with set-window-buffer
to set the desired buffer in the desired window ... the sky is the limit.
Here is another example using the emacs-startup-hook
that kills the *scratch*
buffer if it exists even if it is not presently displayed in a window:
(add-hook 'emacs-startup-hook (lambda ()
(when (get-buffer "*scratch*")
(kill-buffer "*scratch*"))))