If I modify a buffer and then save it, the buffer modified flag resets and the subsequent undo commands set the buffer modified flag to true.

However if I use (set-buffer-modified-p nil), the flag resets but then the first undo command doesn't set it to true.

How can I get the first undo to set the buffer modified flag to true after (set-buffer-modified-p nil)?

2 Answers 2


When running undo, there are two possibilities. Either you're undoing something, or redoing something. You can tell the difference by what gets echoed to the minibuffer.

On my Emacs (24.3.1), after explicitly setting the modified status of the buffer to nil, any "undo" results in setting the buffer status to edited, but "redo" does not change the buffer status. This appears to be a bug in Emacs, and should be reported.


My crystal ball tells me that when you call (set-buffer-modified-p nil) you've only made "one" modification to the buffer since it was unmodified.

So if you hadn't called (set-buffer-modified-p nil), the "undo" would have marked the buffer as unmodified. And as it turns out, calling set-buffer-unmodified has no effect on that part of the behavior: when undoing, Emacs resets the "modified" flag based on entries in the undo-log which say "last time we were here, the buffer was marked unmodified and the associated file had state FOO". So you can have several different states that are all considered as "unmodified" and undo/redo will set the modified flag accordingly when getting to those states.

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.