sligocki
sligocki

Reputation: 6387

How to force Emacs to save even if it thinks (no changes need to be saved)

This happens to me all the time:

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

Upvotes: 18

Views: 4896

Answers (6)

Student
Student

Reputation: 155

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.

Upvotes: 0

GreenMatt
GreenMatt

Reputation: 18590

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.

Upvotes: 0

Joe Casadonte
Joe Casadonte

Reputation: 16889

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.

Upvotes: 0

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19757

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))

Upvotes: 10

scottfrazer
scottfrazer

Reputation: 17337

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))

Upvotes: 18

Tagore Smith
Tagore Smith

Reputation: 1554

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.

Upvotes: 18

Related Questions