4

I often did undo too much, then redo it. I noticed I have default undo position in my mind. I think C-x C-s is helpful but not enough, because it just indicate modified or not, so the buffer is still possible to undo.

So I want to reset undo history when I think this is certainly my default undo position. If I can do so, I think I can come back default undo position easily like this C-u 1000000000 C-_.

For example, I need to find or create command like below xxx.

If I type below text in the current buffer,

...So Emacs is the excellent editor.

then I use M-x xxx. And then, I continue to type like this.

...So Emacs is the excellent editor.
But there are more excellent editor like...

But to pass through the reviewers, I need to correct second one. So I have to do C-u 1000000000 C-_ and the result will be

...So Emacs is the excellent editor.

Is there any command like xxx above?

2 Answers 2

4

Instead of C-u 1000000000 C-_, I'd recommend M-x revert-buffer RET. Then you don't need your xxx command.

As for xxx, you could do:

M-x buffer-disable-undo RET
M-x buffer-enable-undo RET
1
  • But reverting returns to the last save to disk (assuming it's a file buffer). I got the impression that OP wanted to return to some particular state that he considers the "default" or origin, but that that state was not necessarily saved to disk. (I admit that I'm not very sure what the question/request is.)
    – Drew
    Commented Mar 25, 2017 at 5:47
4

You can clear the undo history with this:

(setq buffer-undo-list nil) ; discard undo history
(set-buffer-modified-p nil) ; mark the buffer as unmodified

I have the following functions in my config for this use case. If the buffer is associated with a file then you can set the "undo point" and go back to it with the undo-all command. For buffers without a backing file then setting the undo point also registers the undo-all command as the local revert function. I mostly use this with temporary buffers where I'm working with some chunk of text copied from somewhere else, and find it convenient to use my revert-buffer binding to discard changes and get back to the original text.

(defun my-buffer-undo-all (&optional ignore-auto noconfirm)
  "Undo all edits in the current buffer.
With a prefix argument, do not prompt for confirmation.

This function can be used as a `revert-buffer-function'.  The
argument IGNORE-AUTO is ignored if specified.  Optional second
argument NOCONFIRM means don't ask for confirmation."
  (interactive)
  (or ignore-auto)                      ; silence compiler
  (if (or noconfirm
          current-prefix-arg
          (not (buffer-modified-p))
          (yes-or-no-p (format "Undo all edits? ")))
      (progn
        (when (buffer-modified-p)
          (undo))
        (while (buffer-modified-p)
          (undo-more 1)))))

(defun my-buffer-set-undo-point ()
  "Make the current buffer state the undo point.
Marks the current buffer unmodified, clears the buffer undo list,
and (for non-file buffers) registers a custom `revert-buffer-function'."
  (interactive)
  (setq buffer-undo-list nil)
  (set-buffer-modified-p nil)
  (unless (buffer-file-name)
    (setq-local revert-buffer-function #'my-buffer-undo-all)))
2
  • 1
    Thanks for the code & explanations.
    – phs
    Commented Feb 16, 2019 at 7:00
  • Good one! (setq buffer-undo-list nil) makes the *scratch* buffer ever more useful for experiments.
    – lauir
    Commented Apr 23, 2020 at 14:41

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.