1

By default, a *scratch* buffer is created when Emacs starts. The initial *scratch* buffer contains the following message by default:

;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with C-x C-f and enter text in its buffer.

This message appears to be set by the variable initial-scratch-message.

If I kill (C-x k) the initial scratch buffer and recreate it using C-x b *scratch*, the new scratch buffer will be blank. It will not contain the initial-scratch-message. This is not what I want. I want to see the message. How do I configure all scratch buffers to contain initial-scratch-message when they are recreated?

4
  • Here is a link to an idea you may wish to use to create scratch buffers. You can use (insert ...) to insert text wherever you want, just run a test to see whether the buffer already exists so you don't insert the same text twice. emacs.stackexchange.com/a/16493/2287 You can modify the example to make it be whatever major-mode you want, or even just fundamental-mode if so desired. When the *scratch* buffer is created automatically by Emacs, you may wish to use a custom function attached to the variable initial-major-mode which does more than just set major-mode.
    – lawlist
    Jan 3, 2021 at 17:27
  • NOTE: When the O.P. "recreate[s] it using C-x b *scratch*", the O.P. is merely creating a new generic/vanilla buffer with no particular major-mode. It is just like creating a new buffer named *foo* or *bar* or *baz*. The question, as written, seems to imply that the O.P. believes the newly created buffer named *scratch* may have had some special relationship to the initial *scratch* buffer beyond having the same buffer-name.
    – lawlist
    Jan 3, 2021 at 18:34
  • @lawlist When I recreate a scratch buffer using C-x b *scratch*, I get a Lisp interaction mode buffer. I am using GNU Emacs 25.2.2.
    – Flux
    Jan 3, 2021 at 19:21
  • Thank you -- you are correct. The internal function set-buffer-major-mode within buffer.c will run the function attached to the initial-major-mode variable if the buffer-name is *scratch*. The Lisp function window-normalize-buffer-to-switch-to within window.el calls set-buffer-major-mode. Therefore, creating a custom function and attaching it to the initial-major-mode variable seems like the most appropriate course of action given your desire to use the buffer-name *scratch*. Thank you for providing me with the opportunity/incentive to trace the aforementioned items.
    – lawlist
    Jan 3, 2021 at 20:00

1 Answer 1

0

The internal function set-buffer-major-mode within buffer.c will run the function attached to the initial-major-mode variable if the buffer-name is *scratch*. The Lisp function window-normalize-buffer-to-switch-to within window.el calls set-buffer-major-mode. Therefore, creating a custom function and attaching it to the initial-major-mode variable seems like the most appropriate course of action given the desire by the O.P. to use the buffer-name *scratch*. Using substitute-command-keys in conjunction with the initial-scratch-message will transform \\[find-file] into C-x C-f.

(setq initial-major-mode (lambda ()
                           (insert (substitute-command-keys initial-scratch-message))
                           (lisp-interaction-mode)))

The default value of the variable initial-scratch-message in Emacs 27.1 is:

(purecopy "\
;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with \\[find-file] and enter text in its buffer.

")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.