Instant scratch buffer for current mode

When emacs launches it will typically show the *scratch* buffer, which is in lisp mode by default, and is useful for making rough notes and evaluating bits of lisp code.

You can chose the default mode of the scratch buffer by setting the variable initial-major-mode as described on emacs redux. So if you want it to be in text mode, use

(setq initial-major-mode 'text-mode)

Today I wanted a scratch buffer in org-mode for some rough work with a table. I could have switched to the scratch buffer and used M-x org-mode to switch the major mode of the scratch buffer, but I found a nicer solution. I installed the package scratch and added the following to my emacs config file:

;; multiple scratch buffers                                               ;;
;; uses package "scratch"
(autoload 'scratch "scratch" nil t)

With this package, if you use M-x scratch it will launch a scratch buffer for the current mode. So in my org-mode buffer, using this command gave me a scratch buffer called *org* where I could put my temporary org scribblings.

  • Kaushal Modi

    I didn’t know such a package already existed. I made up this little elisp snippet to switch back and forth between my working buffer and the scratch buffer. By default, it creates a scratch buffer with the current major mode if one doesn’t exists. If one exists, it switches to it. Using universal prefix, I can force to open an org-mode or emacs-lisp-mode scratch buffer.


    • Ben Maughan

      That’s a nice solution – I might prefer it to the scratch.el package for the fact you can use the prefix argument.

      Out of curiosity, how does emacs know that your new buffer is a scratch buffer (i.e. it won’t prompt to save)? Is this because of the “*” in the buffer name?

      • Kaushal Modi

        Are you referring to the save prompt when quitting emacs?