Gih's Blog


Quickly create empty buffer (scratch) in Emacs.

2011-06-27 by gihnius, tagged as emacs

By default when Emacs starts up, it contains a buffer named *scratch*, which is provided for evaluating Emacs Lisp expressions interactively. Its major mode is "Lisp Interaction mode". 

The “*scratch*” “buffer” is designed for emacs lisp programers. (it defaults to lisp-interaction-mode.) but 99% of emacs users are not lisp coders. 

If you want a scratch pad, how do you open a new(empty) document in Emacs? This way is familiar to all software users. Here is a elisp function to let you do this. It open one or more new buffers and set major to text-mode.

(defun new-empty-buffer ()
  (let ((n 0)
    (while (progn
             (setq new-buf (concat "-untitled-"
                                       (if (= n 0) "0" (int-to-string n))
             (incf n)
             (get-buffer new-buf)))
    (switch-to-buffer (get-buffer-create new-buf))

You can change the major mode from the last line to yours, eg. lisp-mode, ruby-mode, html-mode.

Bind this function to Win + n or CMD + n, just like what we do in other softwares.

(global-set-key (kbd "s-n") 'new-empty-buffer)

Then, you can press ⌘+n to create an empty buffer named -untitled-0-, and the next will be -untitled-1-, -untitled-2- ... -untitled-N-.