2

for background, I am an emacs newbie and long time unix user... What I am trying to do is to work in emacs in a pattern that is familiar to someone who has used a windows, linux, mac text editor such as notepad, vi, textwrangler, etc:

  • Open the editor
  • Open a blank buffer if necessary (sometimes called Untitled)
  • Type and edit text
  • Exit the editor after being asked to save the changes to the buffer

In my experience, this is not how it works in emacs, but I am hoping to hear from y'all about how it can be, or how my thinking about editing might be changed for the better. Here is how it seems to work in emacs:

  • Open the editor emacs
  • Open a blank buffer C-x b Untitled
  • Type and edit some text
  • Exit the editor...

Oops, after losing some changes without prompting, I caught on that this wasn't an ideal workflow (call me slow), so I thought about alternatives and tried visiting a new file instead of a new buffer: C-x C-f Untitled, but this meant I had to provide a path and be saddled with it throughout the life of the session - meaning that when exiting, emacs would think that was where it should save the file (reasonable of emacs really). I don't like having to provide this information up-front as I may not want to save my edits later.

So, my question, is it possible to replicate the workflow above in an emacsy way, ie. prompt on changes to a buffer, but only buffers like the described Untitled, not *Messages*, or am I just thinking wrongly and there's a better workflow that I'm somehow overlooking?

If this is duplicated elsewhere, please point that out, I searched, but searches are fickle.

0

2 Answers 2

2

Just open a new buffer that is expected to be associated with a file (since you want to be warned about saving the buffer to a file even before you have ever saved it to a file, i.e., before you have ever associated it with a file).

In other words, use C-x C-f untitled instead of C-x b untitled.

Buffers not associated with files are generally assumed to not be candidates for automatic saving or warning about saving before exiting.

When you use C-x C-f lots of things happen that make the buffer amenable to being saved as a file. That's the right habit to get into (IMHO).

Just because you use C-x C-f, that doesn't mean that you're obliged to save the buffer created. It just means that Emacs puts some ducks in a row to enable you to do that, i.e., Emacs expects that you might want to do it.


Updated to respond to your comment about "need[ing] to decide before editing what the file is named and where it will be located":

The name you give it will be used by default when you save (C-x C-s). But you can always instead write the buffer to a different file name and location, using C-x C-w (write-file).

(You can also use C-x b and then use C-x C-w later. But in the meantime Emacs will not prompt to save the buffer when you exit. It assumes that you probably do not want to save the buffer -- if you did then you probably would have saved it at least once, using C-x C-w, or you would probably have used C-x C-f to start with.

The big difference (one big difference, anyway) between Emacs and the editors you are presumably used to is that Emacs users often have buffers that they do not intend to save to disk files. In fact, they often have lots of such buffers. Buffers associated with files are only one kind of buffer in Emacs (or rather, associating a buffer with a file is only one thing you can do with it).

It's pretty simple really, though it might not be what you're used to. The default location is your current location in the file system (default-directory). There is no default file name -- you give it whatever name you want. You can immediately specify any location or accept the current location (default) for the time being.

If the file name you give it initially happens to be recognized as one taking a certain mode, that mode is automatically used for it -- see C-h v auto-mode-alist. If the name you give it is not so recognized then you get your preferred default mode. If you have never established your own preferred default mode then you get fundamental-mode, which doesn't do much (by design).

4
  • what I dislike about this method is the need to decide before editing what the file is named and where it will be located. i.e. if I call it untitled, that's not likely to be useful when I decide to save, but it's fine while hacking the text together and when I'm prompted to save, there doesn't appear to be a facility to rename or save somewhere else.
    – decuser
    Commented Sep 28, 2017 at 3:52
  • 1
    I've updated the answer to speak to your dislike, i.e., what you're used to, and how and why Emacs is different.
    – Drew
    Commented Sep 28, 2017 at 4:19
  • I already accepted phils' answer, but your explanation of the emacs difference is particularly clear and I would accept it as well.
    – decuser
    Commented Sep 28, 2017 at 17:21
  • You can accept only one answer. You can change which answer you accept anytime. (I'm not suggesting you do that here; just letting you know.)
    – Drew
    Commented Sep 28, 2017 at 20:01
2

You could try (setq-default buffer-offer-save t) but that's going to be really noisy, so you really want a way of deciding which of the non-file-visiting buffers are worth asking about, and which are not.

I suggest using a custom command for creating such buffers. e.g.:

(defun my-new-buffer (name)
  "Create a new buffer which must be saved when Emacs exits."
  (interactive "BNew buffer name: ")
  (let ((buf (generate-new-buffer name)))
    (with-current-buffer buf
      (setq buffer-offer-save t))
    (switch-to-buffer buf)))

(global-set-key (kbd "<f6>") 'my-new-buffer)

n.b. Unmodified empty buffers will still be ignored.

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.