Emacs: New Empty Buffer

By Xah Lee. Date: . Last updated: .

To open a new buffer/file in emacs, call 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.

URL `http://ergoemacs.org/emacs/emacs_new_empty_buffer.html'
Version 2016-12-27"
  (interactive)
  (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 calling describe-variable on auto-mode-alist.

Open File topics

  1. Emacs: File Management (dired tutorial)
  2. Emacs: Bookmark Tutorial
  3. Emacs: Open Recently Opened File
  4. Emacs: Open File Fast
  5. Emacs: Open Last Closed File
  6. Emacs: Open File Path Under Cursor

Buffer topics

  1. Emacs: List Buffers
  2. Emacs: Switch Buffer
  3. Emacs: New Empty Buffer
  4. Emacs: Next/Previous User Buffer
Like it? Buy Xah Emacs Tutorial. Thanks.