29

Just re-create the buffer by switching to it: C-x b *scratch* RET. Since Emacs 21.2, if you create a buffer called *scratch*, it's automatically put in Lisp Interaction mode.


26

It will be convenient to bind the below function to a key binding of your choice. If you are currently working on a FILE buffer, calling the below function will toggle between FILE's major-mode specific *scratch* buffer called *scratch-MAJOR-MODE* and the FILE buffer. Given the example in question, if I am working on a Perl script called myperl.pl, calling ...


14

You can (ab-)use kill-buffer-query-functions for this purpose: (add-hook 'kill-buffer-query-functions #'my/dont-kill-scratch) (defun my/dont-kill-scratch () (if (not (equal (buffer-name) "*scratch*")) t (message "Not allowed to kill %s, burying instead" (buffer-name)) (bury-buffer) nil)) In my old Emacs configuration I used this to ...


11

As mentioned in the comments you can tweak eval-expression-print-* or expand by hitting RET or mouse-2 on the ellipses (which calls last-sexp-toggle-display). However probably the most useful for general messing around in the *scratch* buffer is: (pp package-alist) Which will format the output in a more readable way.


11

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 ...


11

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 ...


7

It's arguably better to re-create the *scratch* buffer automatically whenever it gets deleted, and avoid having to retype its name. (defun prepare-scratch-for-kill () (save-excursion (set-buffer (get-buffer-create "*scratch*")) (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer t))) (defun kill-scratch-buffer () (let (kill-buffer-query-...


6

Set eval-expression-print-length and eval-expression-print-level to nil.


6

Just redefine the function with its original definition again. You'll probably find this article very helpful: https://masteringemacs.org/article/evaluating-elisp-emacs You can't 'undo' lisp evaluation like you can undo text editing -- Emacs isn't keeping a history of all previous states of the lisp interpreter (it couldn't do so -- the idea just isn't ...


5

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.


5

Check the value of option initial-major-mode: M-x customize-option RET initial-major-mode RET. Its default value is lisp-interaction-mode, which puts *scratch* in that mode. C-h v initial-major-mode tells you this: Major mode command symbol to use for the initial *scratch* buffer. You can customize this variable. So remove the customization that ...


4

GNU Emacs default bindings: C-xb*scratch* or, more verbosely M-xswitch-to-buffer Typing: *scratch* And then hitting the RET key. The *scratch* buffer is the buffer selected upon startup, and has the major mode Lisp Interaction. Note: the mode for the *scratch* buffer is controlled by the variable initial-major-mode. In general you can create as many "...


4

format generates a string, but it doesn't do anything with it. You probably want to pass the result of format on to something that does: (defun mt () (dotimes (x 20) (dotimes (y 20) (insert (format "%3d " (* (1+ x) (1+ y))))) (insert (format "\n")))) UPDATE I think you're also confused about how dotimes works. From the doc string: (...


4

A new feature has been introduced for persistent scratch called "remember" From https://www.masteringemacs.org/article/whats-new-in-emacs-24-4 The new command ``remember-notes`` creates a buffer which is saved on ``kill-emacs``. You may think of it as a \*scratch\* buffer whose content is preserved. In fact, it was designed as a replacement for \*...


3

My original suggestion to the OP based on my own slightly misguided experimentation was this: make sure that the mode you want to set as the initial-major-mode is loaded before you set initial-major-mode with (setq initial-major-mode 'name-of-mode). This seems to have solved the problem that the OP was having. However, I have also tested changing my entire ...


3

defun and while are macros, and are highlighted as such. set and + are functions, and are not highlighted like macros. Old versions of Emacs seem to have a list of specific macros to highlight, but more recent (possibly unreleased?) versions simply highlight all known macros. Here are the docs on functions and macros so you can tell them apart. I admit, ...


3

You'll notice that you're using a variable called auto-save-file-name-transforms. Since *scratch* has no filename, it falls back to autosaving in default-directory. For the *scratch* buffer, or any other buffers like this with no filename, you can use: (setq-local default-directory "~/.emacs.d/data/autosave") Edit: I see that you don't really care about ...


2

I like to use scratch buffers for throw-away stuff. Having only one is somewhat limiting though, therefore I have scratch as an interactive command for opening a new scratch buffer (no matter how many exist already): (defun scratch () "create a new scratch buffer to work in. (could be *scratch* - *scratchX*)" (interactive) (let ((n 0) bufname)...


2

I made it slightly different (but the previous answers helped me a lot, thanks guys), to avoid string matching. I modified the "new-empty-buffer" function suggested by Xah Lee from ErgoEmacs, making it set a local-variable 'custom-scratch-buffer when the buffer is created. Therefore, we know when the buffer is a scratch buffer created by this function and ...


2

I guess much of this is a matter of taste and usage patterns. I tend to use something inbetween your solution and @Drew's usage (according to the comments). Here is the gist of it: I want "throwaway" buffers to be quickly available, without having to think ahead of time where to save them; I want them to be file buffers in order for Emacs to warn me if I ...


2

Try this: C-x b (type a name for your new scratch buffer, e.g. "asdf") 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.


2

In step 23 of the startup sequence emacs inserts initial-scratch-message into the *scratch* buffer. That step is after loading the init file, after running after-init-hook, and before emacs-startup-hook. Ergoemacs uses its own themes defined with ergoemacs-theme. The standard theme is defined in library ergoemacs-themes.el. The value of initial-scratch-...


2

I'd probably use a different approach, using mapconcat to apply the function and concatenate the results: (let ((range (number-sequence 1 20))) (mapconcat (lambda (y) (mapconcat (lambda (x) (format "%03d" (* x y))) range " ")) range "\n")) This returns the string that you can insert or whatever else.


2

This will do the job. (let ((filename "~/.emacs.d/startup.txt")) (when (and (file-exists-p filename) (get-buffer "*scratch*")) (with-current-buffer "*scratch*" (erase-buffer) (insert-file-contents filename))))


2

initial-buffer-choice allows to specify a path to a file or directory: (let ((filename "~/.emacs.d/startup.txt")) (when (file-exists-p filename) (setq initial-buffer-choice filename)))


2

If you just want to put *scratch* in its own frame and use special frame parameters for it (e.g. give it its own background color, font, or whatever) then you can do this just by doing these two things: Customize special-display-alist to have the frame parameters you want. Customize special-display-buffer-names, adding *scratch* to it. In a comment you ...


2

There is no need to provide the "." in your command. However, by default, there is a startup message shown. emacs -nw --eval '(setq inhibit-startup-message t)' or emacs -nw --no-splash should do what you want.


1

This can be done using initial-buffer-choice, While initial-buffer-choice can be set to a filename, this will load the file as well as any files passed via the command line (splitting the window or not even showing the buffer depending on your setup).So reading file data into *scratch* buffer has the advantage that exact behavior is preserved, just ...


1

The function assigned to the variable initial-major-mode is only called when *Scratch* is created by Emacs (e.g., startup.el, or when certain criteria is met when killing a buffer). It can be a custom function -- e.g., set the major-mode and do anything else under the sun. (setq initial-major-mode (lambda () (lisp-interaction-mode) (message "*scratch*")))...


1

I had a similar problem getting web-mode to play nice with Django templates. For me the fix was as simple as adding the following to my init file: (setq web-mode-engines-alist '(("django" . "\\.html\\'")) ) For you, try adding something like: (setq web-mode-engines-alist '(("dust" . "\\.html\\'")) ) For more details, see the section ...


Only top voted, non community-wiki answers of a minimum length are eligible