The typical workflow in other text editors is that you are presented with a blank window, to start typing into, and then when you exit the editor (or kill the buffer if applicable, close the window in some editors - in general any action that would normally prompt to save), if you have typed anything, it will prompt you to save modifications and prompt you for a filename if you answer yes.

Is there a way to replicate this workflow in Emacs - i.e., have a buffer that does not have a filename (the buffer name would be something like *unsaved file*, I suppose), but prompts to save?

Creating a buffer by typing an arbitrary name after C-x b results in a buffer that won't prompt to save.

  • One way to go about it would be to add a hook to kill-buffer-hook to prompt you for specifying the file to write the contents to, however you'd need to be careful about it: from Emacs standpoint there is no difference between a buffer you create by typing C-x b and many other technical buffers created by programs which run in Emacs. So, maybe you'd need to design a command for creating a new buffer with this hook set up.
    – wvxvw
    Commented Sep 4, 2015 at 19:54
  • @wvxvw I was certainly expecting to have to define a command - I'd just hoped there was a property that already existed "this buffer will need to be saved" that I could leverage.
    – Random832
    Commented Sep 4, 2015 at 19:55
  • Is there a reason not to name the file up front -- i.e. use C-x C-f foo to create your buffer and start typing,. When you kill the buffer you'll be prompted to save, and can either continue with the name you picked or at that point cancel and use C-x C-w to save it somewhere more appropriate.
    – glucas
    Commented Sep 4, 2015 at 19:58
  • 1
    @glucas there are a few reasons why I don't use the workflow you describe: one is because I tend to frequently save the buffer (call it a C-x C-s OCD if you like) and this would cause the file to be created with the random name I typed first. The other reason is because when using M-x compile or other such commands (which internally run save-some-buffers), you're always prompted to save any modified buffer associated to a file. Commented Sep 4, 2015 at 20:27
  • @Francesco +1 Actually I agree, and create lots of temp buffers not associated with a file. Most of the time I never want to save those though, so having to decide between "create a scratch buffer" and "create a scratch buffer but warn me if I close it" seems like more friction than using C-x b for temp buffers and C-x C-f for stuff I might want to save. Personal works flows vary, of course!
    – glucas
    Commented Sep 4, 2015 at 21:28

5 Answers 5


You can create this yourself by using a new command to open "New File" buffers, and using a custom kill-buffer-hook to ensure they get saved before you close them.

(defvar-local is-new-file-buffer nil)

(defun save-new-file-before-kill ()
  (when (and (not (buffer-file-name))
              "New file has not been saved. Would you like to save before closing?"))
    (call-interactively 'save-buffer)))

(add-hook 'kill-buffer-hook 'save-new-file-before-kill)    

(defun new-file (dir)
  (interactive "DCreate New File In: ")
  (let ((buffer (generate-new-buffer "<Unsaved File>")))
    (switch-to-buffer buffer)
    (setq-local default-directory dir)
    (setq-local is-new-file-buffer t)))

Using this, you can call new-file to create a new unsaved buffer in a specified directory, then you can save it anytime you want. When you try to kill a unsaved buffer created with new-file emacs will prompt you for a file name to save to before it kills the buffer.

  • 1
    Nice and simple idea, but I'm not sure this is enough: kill-buffer-hook doesn't seem to run when quitting emacs. Commented Sep 4, 2015 at 21:02
  • add a kill-emacs-hook Commented Sep 4, 2015 at 21:04
  • 1
    yes, of course. I should have thought about it. buffer-offer-save could be another, more standard way to deal with emacs' exit. Commented Sep 4, 2015 at 21:17
  • 1
    and maybe you'd want to (put 'is-new-file-buffer 'permanent-local t) so that changing the buffer's major mode doesn't kill the variable. Commented Sep 4, 2015 at 21:17
  • Good thoughts @Francesco, i've incorporated them into the answer. Commented Sep 5, 2015 at 0:19

Other editors are, well, wrong. You don't want to do that. Emacs is all about buffers, not files.

This question is a duplicate of the question here. And the answer is here.

But since it is not verboten (though it is discouraged) to post the same question here as a question on Stack Overflow, I'll summarize the message I posted there.

Give it a name. Just don't save it.

Not what you wanted to hear, but this is the Emacs way.

Use C-x C-f, giving the (to-be-file-visiting) buffer a name. Edit the text. Do not use C-x C-s to save the buffer to the file (i.e., to disk).

End of story.

If you don't want to take the chance of accidentally hitting C-x C-s and thus saving your edits, then use C-x b instead of C-x C-f. You are (even here) prompted for the buffer name. Giving it a new name (not the name of an existing buffer) creates a new buffer. In this case, if you use C-x C-s then Emacs prompts you for the file location to save the buffer in.

Why would you want to use C-x C-f instead of C-x b, if you might not want to save the buffer? Providing a file extension in the file name you give automatically puts the buffer in the proper major mode (typically). Otherwise (for C-x b) you need to put the buffer in the mode you want.

  • 1
    If you create a file-visiting buffer with C-x C-f, emacs will prompt to save it every time it runs an external process (such as when compiling, grepping, etc.) Commented Sep 4, 2015 at 20:59
  • 1
    @Francesco: Yes, which is a good thing. And it is true only IF the buffer is actually visiting a file. If the buffer has never been saved and the file does not exist then you are not prompted to save it. IOW, exactly the same as the throw-away buffer the OP asked for.
    – Drew
    Commented Sep 4, 2015 at 21:12
  • "If the buffer has never been saved and the file does not exist then you are not prompted to save it". I just made the test (with emacs -Q) and yes, M-x compile prompts to save the buffer event if it is associated to a file which doesn't exist yet. Commented Sep 4, 2015 at 21:26
  • @Francesco: You previously made general statement about any external command. The behavior depends on the particular Emacs command. Every command is different. And you can write your own command that has any behavior you like in this regard. And no, if option compilation-ask-about-save is nil then you are not asked anything by compile. In that case, the buffer is saved silently. compile is a general command that can execute any shell command/script, so it assumes that the command is acting on a file. And no, grep, which you also mentioned, does not prompt to save the buffer.
    – Drew
    Commented Sep 4, 2015 at 23:12
  • @Francesco: Besides which, the condition you quote from me, "if the buffer has never been saved..." was in response to your claim about C-x C-f (find-file). You then suggest that this condition is contradicted by M-x compile. find-file is not compile. Each command decides the behavior appropriate for it (which is good).
    – Drew
    Commented Sep 5, 2015 at 2:03

As far as I know, there is no standard way to have the behaviour you describe in stock Emacs. This is seen by many as a problem, and the Internet features some interesting discussions about the way new files/scratch buffers could/should be handled by Emacs (see for example Xah Lee's essay about the scratch buffer.

disclaimer: what follows is self-advertisement!

I have created my own package to deal with scratch buffers. Be forewarned that it is probably not ready for public consumption yet. Still, you can find it on github if you want to give it a try.


Have you tried disabling ido-mode? The default emacs (without ido-mode) does not ask for a filename. C-x b, type any name for buffer, and off you go.

  • 1
    If I create a buffer this way it will not prompt me to save it when I kill it or exit emacs.
    – Random832
    Commented Sep 4, 2015 at 20:55
  • 1
    You can control that behavior through some global variables. Remember the whole philosophy of emacs is to be able to edit buffers without being tied down to a file on the operating system. It's wrong to assume you need a valid file all the time.
    – Emacs User
    Commented Sep 4, 2015 at 20:58

I tend to use scratch.el when I need a disposable buffer (where disposable means: a buffer I do not care to save in a file).

This is my setup:

(use-package scratch ; Mode-specific scratch buffers
  :ensure t
  :bind ("C-c b s" . scratch))

The command scratch generates a mode-specific buffer. So, for instance, if I am editing an Org file, scratch will visit a new buffer with Org-mode as its default major mode.

I can still save it in a file, if at all needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.