TransWikia.com

Can't get rid of scratch buffer at startup for Emacs 26.3

Emacs Asked on September 2, 2021

I am using GNU Emacs 26.3 (build 1, x86_64-w64-mingw32) of 2019-08-29.
In my .emacs I specifically asked not to show the startup screen or buffer *scratch*:

(custom-set-variables
 '(inhibit-startup-screen t)
 '(initial-buffer-choice nil))

However, whenever I open a file with emacs (or runemacs), the window is split in half, showing both the *scratch* buffer and the file I opened. How do I get rid of showing the *scratch* buffer when I start Emacs?

One Answer

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*"))))

Answered by lawlist on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP