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?

2
  • 5
    Is there anything inconvenient with doing M-x org-mode when you're in *scratch* buffer?
    – xji
    Commented Sep 17, 2015 at 7:50
  • 1
    @XiangJi Didn't even think to do that. That's a great idea. Commented Sep 17, 2015 at 7:51

4 Answers 4

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))) ))))
6
  • So would it be possible to have two *Scratch* buffers then? Commented Sep 10, 2015 at 5: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
    Commented Sep 10, 2015 at 5:52
  • Very cool. Thanks for looking at this! Commented Sep 10, 2015 at 5: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
    Commented Sep 10, 2015 at 6:08
  • When we call my-scratch-buffer can it be open in full window instead of splitting normal window in to half size?
    – alper
    Commented Apr 17, 2023 at 9:36
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).

4
  • 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
    Commented Sep 18, 2015 at 0: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. Commented Sep 18, 2015 at 6: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
    Commented Sep 18, 2015 at 7:58
  • 2
    By the way, there is a package on MELPA called persistent-scratch which claim to create persistent scratch buffers.
    – Vivi
    Commented Sep 18, 2015 at 8: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.

2
  • The simplest way by far.
    – user2699
    Commented Sep 17, 2015 at 15:39
  • This is certainly useful. I like having a scratch buffer exclusively devoted to org-mode since I often use the scratch buffer for other things. Commented Sep 18, 2015 at 4:44
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.

Your Answer

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

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