Skip to content

Instantly share code, notes, and snippets.

@eev2
eev2 / create-scratch-buffer.el
Last active March 17, 2022 19:56
Create a new scratch buffer in emacs
(defun create-scratch-buffer (&optional nomode)
"Create a new scratch buffer and switch to it. If the region is active, then
paste the contents of the region in the new buffer. The new buffer inherits
the mode of the original buffer unless nomode is set.
Return the buffer."
(interactive "P")
(let (bufname (mjmode major-mode) (paste (and (region-active-p) (prog1 (buffer-substring (mark t) (point)) (deactivate-mark)))))
(if (and (not nomode) (boundp 'ess-dialect) ess-dialect)
(setq mjmode (intern-soft (concat ess-dialect "-mode"))))
(setq bufname (generate-new-buffer-name "*scratch*"))