18

This happens to me all the time:

  • I have a file open in emacs,
  • I save it ('save-buffer),
  • the file changes on disk (or get's deleted, moved, etc.)
  • but I want it back, so I try to save again in emacs ('save-buffer) and instead of saving it says "(no changes need to be saved)" and does nothing.

Is there a different function, or a setting, that I can use to force emacs to save?

0

6 Answers 6

18

Wrap a function around save-buffer that marks the buffer modified first:

(defun save-buffer-always ()
  "Save the buffer even if it is not modified."
  (interactive)
  (set-buffer-modified-p t)
  (save-buffer))
1
  • 1
    Note for users of evil, you will want to add this to your config: (evil-declare-abort-repeat 'save-buffer-always) - otherwise saving will clobber your last action. See: emacs.stackexchange.com/questions/33287 `
    – ideasman42
    Jun 4, 2017 at 17:11
18

You can save as, with C-x C-w. That should save unconditionally. You can also just type a space then backspace over it. Emacs is smart enough to realize that if you undo everything you've done so far the buffer has no changes, but if you make changes and then manually reverse them it will consider the buffer to have been changed.

2
  • Yeah, thanks, I've been using these, but it's sort of annoying. Particularly for C-x C-w, you need to retype the filename and press ENTER. I'd love to find some thing that I can just set to a key-binding and have one-click saving.
    – sligocki
    Jul 9, 2010 at 21:11
  • 3
    @sligocki: I've found that Emacs will re-use the existing filename if you just press <enter> at the filename prompt without typing anything.
    – GreenMatt
    Nov 19, 2014 at 21:31
10

You can mark the current buffer as modified using the Emacs-Lisp function not-modified with a prefix arg, bound to:

C-u M-~

The answer above won't work if you don't call the new function directly. If you want to seamlessly change emacs saving behavior. The best solution is to create an advice:

(defadvice save-buffer (before save-buffer-always activate)
  "always save buffer"
  (set-buffer-modified-p t))
3
  • Nice, thanks! I think that's equivalent to the function below, so I'm going to use the function (so I can assign it a key).
    – sligocki
    Jul 10, 2010 at 1:15
  • Ah, never heard of advice before, I assume these are some sort of hooks to run before save? Sounds good, thanks for pointing me to it. Now I have to choose between remapping the key-combo to run scott' save-buffer-always or using advice ... :)
    – sligocki
    Jul 12, 2010 at 17:59
0

As a slight alternative to scottfrazer's answer:

(defun my-save-buffer-always-sometimes (prefix)
  "Save the buffer even if it is not modified."
  (interactive "P")
  (when prefix
    (set-buffer-modified-p t))
  (save-buffer))

This was you can force it when you want to with a prefix (C-u C-x C-s) but not unnecessarily change a file otherwise. The last modified timestamp is very useful (e.g. source code control) that it seems a shame to change it arbitrarily. YMMV, of course.

1
  • Thanks for the note. However, in my case, if I explicitly ask emacs to save, I want it to save and update timestamp, etc. In fact, timestamp assumptions appear to be the problem in the first place, because I'm using git and git branch will change the files in the directory without updating the timestamp causing emacs to think that it is still the same old file :/
    – sligocki
    Jul 11, 2010 at 5:17
0

A similar problem brought me online to look for a solution. Then it hits me that all I have to do is type a space (or any character) and delete it, which marks the buffer as changed. Then I can type C-x C-s as normal. Maybe not sophisticated or advanced, but it works.

0

Like Tagore Smith said, you can force Emacs to save the buffer with C-x C-w.

If you're using Evil mode, you can also achieve this behavior by typing :w! in normal state. Unlike C-x C-w, :w! will not prompt you for the filename to save to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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