16

While working on a document in emacs I often find myself opening a new buffer, doing something in org-mode, copying the org-mode content, and pasting it into my document. I used to create a new org-mode document every time I needed to do this. Now I have a file ~/.scratch.org where I keep my this work.

Would it be possible to create an *org-scratch* buffer that behaved analogously to the *scratch* buffer?

Brian Fitzpatrick
  • 2,265
  • 1
  • 17
  • 40

4 Answers4

16

The initial major-mode for the *Scratch* buffer is controlled by the variable initial-major-mode -- the value needs to be a symbol (which in layman's terms means put a single quote in front of the major-mode name): http://www.gnu.org/software/emacs/manual/html_node/elisp/Auto-Major-Mode.html

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

EDIT: Based on a comment of the original poster, here is a sample function to create non-file-visiting buffers in sequential order with the major-mode of org-mode:

(defun my-scratch-buffer ()
"Create a new scratch buffer -- \*hello-world\*"
(interactive)
  (let ((n 0)
        bufname buffer)
    (catch 'done
      (while t
        (setq bufname (concat "*hello-world"
          (if (= n 0) "" (int-to-string n))
            "*"))
        (setq n (1+ n))
        (when (not (get-buffer bufname))
          (setq buffer (get-buffer-create bufname))
          (with-current-buffer buffer
            (org-mode))
          ;; When called non-interactively, the `t` targets the other window (if it exists).
          (throw 'done (display-buffer buffer t))) ))))
lawlist
  • 18,826
  • 5
  • 37
  • 118
  • So would it be possible to have two `*Scratch*` buffers then? – Brian Fitzpatrick Sep 10 '15 at 05:47
  • I added a sample function that creates new non-file-visiting buffers in numerical order, and I included an `org-mode` designation. The name can be changed from `hello-world` to anything the user desires that is recognized by the operating system -- e.g., Windows doesn't like astrisks. – lawlist Sep 10 '15 at 05:52
  • Very cool. Thanks for looking at this! – Brian Fitzpatrick Sep 10 '15 at 05:54
  • I am happy to have been able to help. I decided to throw the result and display the buffer in one fell swoop -- the functionality is the same, but the function looks a little more compact this way. :) – lawlist Sep 10 '15 at 06:08
  • When we call `my-scratch-buffer` can it be open in full window instead of splitting normal window in to half size? – alper Apr 17 '23 at 09:36
  • @alper -- If you wish to target the currently selected window, then you can change `(display-buffer buffer t)` to `(switch-to-buffer buffer)` – lawlist Apr 17 '23 at 15:52
10

There's an extension called scratch, which allows creating mode-specific scratch buffers. It is available from MELPA, so you should be able to install it easily.

With this package installed, when you are in an org-mode buffer, you can run M-xscratch to get a scratch buffer in org-mode.

If you give a prefix argument, you get the opportunity to choose the mode (instead of selecting the currently active major mode).

François Févotte
  • 5,917
  • 1
  • 24
  • 37
  • I tried this but I think the scratch buffer gets deleted after you quit emacs. The original scratch buffer doesn't, it saves itself automatically too. Is this right or did I miss something? – Vivi Sep 18 '15 at 00:35
  • 1
    Unless you do something special, the original `*scratch*` buffer doesn't persist across sessions, nor is it saved when you exit emacs. Try running `emacs -q` to get the standard behaviour, unmodified by your init file. – François Févotte Sep 18 '15 at 06:37
  • Interesting, I didn't know that. I have just investigated this and I found out it is a feature of Aquamacs. I also found out that Aquamacs comes with the keybinding Command-N to create a new scratch in another frame (this one not persistent across sessions). – Vivi Sep 18 '15 at 07:58
  • 2
    By the way, there is a package on MELPA called persistent-scratch which claim to create persistent scratch buffers. – Vivi Sep 18 '15 at 08:09
6

I think one simple way to achieve it is simply to run M-x org-mode when you're in *snatch* buffer, and thus enabling org-mode directly. This can be applied to enabling any major mode.

xji
  • 2,545
  • 17
  • 36
2

Try this:

  1. C-x b (type a name for your new scratch buffer, e.g. "asdf")
  2. M-x org-mode (change "asdf" buffer's major mode)

When closing this buffer you won't get a query for saving its contents. I.e. you get a proper scratch buffer. And the *scratch* buffer remains intact.

undostres
  • 1,793
  • 12
  • 15