Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: How do I create a new "empty" buffer whenever creating a new frame?

Tags:

emacs

My Emacs is on OS X system. Is there any way to make a new frame defaulted to an empty buffer whenever I use ⌘N (just like the way TextEdit works)? I prefer to write contents first and decide an appropriate filename later. However, Emacs wants me to decide the filename first and write contents later. I don't see any advantage for it. Does anyone know why Emacs works that way?

Basically, if I use C-x 5 2, Emacs always pops up a frame with whatever file I am currently working on. This is inconvenient. I also don't want my Emacs to pop up a new frame defaulted to *scratch* (many Google search results somehow suggest this approach). I prefer it to have a buffer temporarily called "Untitled" in the new frame, and if I use ⌘N again, Emacs pops up another temporarily "Untitled 2" buffer, and so on. In this way, I can decide the buffer filenames later.

like image 962
Bart Simpson Avatar asked Sep 11 '14 15:09

Bart Simpson


People also ask

Can you have more than one buffer in Emacs?

When Emacs has multiple windows, each window has a chosen buffer which is displayed there, but at any time only one of the windows is selected and its chosen buffer is the selected buffer. Each window's mode line displays the name of the buffer that the window is displaying (see section Multiple Windows).

Can only have one buffer open in Emacs at a time?

Each Emacs window displays one Emacs buffer at any time. A single buffer may appear in more than one window; if it does, any changes in its text are displayed in all the windows where it appears. But the windows showing the same buffer can show different parts of it, because each window has its own value of point.

How many buffer can you open at a time in Emacs?

The number of buffers you can have really has no limit. Most of the time, only one or two buffers are displayed, but even if you can't see them, all the buffers you create in an Emacs session are still active. You can think of them as a stack of pages, with the one being displayed as the top page.

How do you change from one buffer to another?

For conveniently switching between a few buffers, use the commands C-x LEFT and C-x RIGHT . C-x LEFT ( previous-buffer ) selects the previous buffer (following the order of most recent selection in the current frame), while C-x RIGHT ( next-buffer ) moves through buffers in the reverse direction.


2 Answers

You can create new buffers with switch-to-buffer. Type C-x b, enter a buffer name, and press RET. If no buffer with that name exists, Emacs creates a new one automatically in Fundamental Mode. You may switch to any other mode as usual with M-x, e.g. M-x python-mode. To change the default buffer, set the default value of major-mode to the desired buffer.

If you'd like to have a buffer name chosen automatically, and create a new frame, however, you need to write your own command:

(defun lunaryorn-new-buffer-frame ()
  "Create a new frame with a new empty buffer."
  (interactive)
  (let ((buffer (generate-new-buffer "untitled")))
    (set-buffer-major-mode buffer)
    (display-buffer buffer '(display-buffer-pop-up-frame . nil))))

Bind this to C-c n:

(global-set-key (kbd "C-c n") #'lunaryorn-new-buffer-frame)

Now pressing C-c n creates a new frame with a new empty buffer named “untitled” where x is a consecutive number.

like image 78
lunaryorn Avatar answered Oct 18 '22 14:10

lunaryorn


The following will create a buffer with a unique name. The buffer is not associated with any file, so if/when you ever C-x C-s save-buffer, you will be prompted to supply a filename.

(defun empty-frame ()
  "Open a new frame with a buffer named Untitled<N>.

The buffer is not associated with a file."
  (interactive)
  (switch-to-buffer-other-frame (generate-new-buffer "Untitled")))
like image 31
Greg Hendershott Avatar answered Oct 18 '22 15:10

Greg Hendershott