Emacs: New Empty Buffer

By Xah Lee. Date: . Last updated: .

To open a new buffer/file in emacs, Alt+x switch-to-bufferCtrl+x b】, then type a new name. [see Emacs: Switch Buffer]

Here's a command to open a new empty buffer, without prompting for a name.

(defun xah-new-empty-buffer ()
  "Create a new empty buffer.
New buffer will be named “untitled” or “untitled<2>”, “untitled<3>”, etc.

It returns the buffer (for elisp programing).

URL `http://ergoemacs.org/emacs/emacs_new_empty_buffer.html'
Version 2017-11-01"
  (let (($buf (generate-new-buffer "untitled")))
    (switch-to-buffer $buf)
    (funcall initial-major-mode)
    (setq buffer-offer-save t)

You can give it a easy key such as F7. [see Emacs: How to Define Keys]

Set Default Major Mode

To set the default mode, put this in your emacs init:

(setq initial-major-mode (quote text-mode))

If you want the init to be the same as emacs scratch buffer, use lisp-interaction-mode. Or, you can use normal emacs-lisp-mode.

List Major Modes

You can view a list of major mode names by Alt+x describe-variable on auto-mode-alist.

Start Emacs with Empty Buffer

(setq initial-buffer-choice 'xah-new-empty-buffer)

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

ELisp Examples

Xah Commands

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples