6

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.

Random832
  • 578
  • 3
  • 11
  • 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 Sep 04 '15 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 Sep 04 '15 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 Sep 04 '15 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. – François Févotte Sep 04 '15 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 Sep 04 '15 at 21:28

5 Answers5

6

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))
             is-new-file-buffer
             (yes-or-no-p 
              "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.

Jordon Biondo
  • 12,455
  • 2
  • 43
  • 62
4

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.

Drew
  • 77,472
  • 10
  • 114
  • 243
  • 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.) – François Févotte Sep 04 '15 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 Sep 04 '15 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. – François Févotte Sep 04 '15 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 Sep 04 '15 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 Sep 05 '15 at 02:03
  • OK. Just so that things are straight and precisely expressed, do we agree that if a buffer is created using find-file on a non existing file, and the user then runs compile, emacs' behaviour is one of the two options: (i) either compilation-ask-about-save is true, and you are prompted to save it or (ii) compilation-ask-about-save is nil, and the buffer is silently saved (meaning that the file should have been aptly named from the beginning). This is precisely the reason why creating temporary buffers with find-file doesn't fit MY workflow (but I'm sure it fits yours...) – François Févotte Sep 05 '15 at 07:56
  • And yes, you're right, I've been overgeneralizing when speaking of grep prompting to save non-existent files. The situations I know where Emacs would prompt you to save modified buffer associated to non-existent files are (re)compile and magit-status (which, as you noted, is good ; it's just not what I expect for possibly-throw-away buffers). – François Févotte Sep 05 '15 at 08:01
  • @Francesco: Yes, that's what I said (and Emacs says): nil compilation-ask-about-save silently saves the buffer before compiling. This applies no matter how the buffer was created, including by C-x C-f (find-file). – Drew Sep 05 '15 at 18:01
2

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.

François Févotte
  • 6,007
  • 1
  • 25
  • 38
1

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.

Emacs User
  • 5,643
  • 18
  • 48
  • 1
    If I create a buffer this way it will not prompt me to save it when I kill it or exit emacs. – Random832 Sep 04 '15 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 Sep 04 '15 at 20:58
0

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.

Manuel Uberti
  • 3,250
  • 19
  • 37