tags:

views:

159

answers:

4

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?

+3  A: 

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.

T Duncan Smith
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
+3  A: 

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))
Jürgen Hötzel
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
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
Exactly: http://www.gnu.org/s/emacs/manual/html_node/elisp/Advising-Functions.html
Jürgen Hötzel
+5  A: 

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))
scottfrazer
Great, that looks like it'll do the trick. Thanks!
sligocki
A: 

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.

Joe Casadonte
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