2

revert-buffer is often slower than closing an opening the same file.

I've noticed - for example, I can undo to the state of the file before reverting.

While this might be useful in some cases, I would like to clear undo history and load the file as if I'd just opened emacs.

Some possible solutions:

Fast Revert

  • Disable undo.
  • Clear undo history, jump list ... etc.
  • Clear the buffer.
  • Load the data from the file into the buffer.
  • Re-enable undo.

Fake Revert

  • Store the scroll & cursor position.
  • Close the buffer.
  • Open the buffer.
  • Restore the scroll & cursor position.

While I could write this, it seems like there may be a more elegant solution then writing my own fast-revert-buffer function.

Does Emacs provide a way to do this already?

4
  • Not an answer to your question, but in terms of undo, the previous history would be irrelevant -- as such, the undo history would be completely reset to zero / nil upon a revert.
    – lawlist
    Oct 17, 2019 at 6:30
  • Just a question: does using global-auto-revert-mode with auto-revert-interval set to a small value (I set it at 1) help? Oct 17, 2019 at 9:21
  • Id rather keep reverting to be a manual operation.
    – ideasman42
    Oct 17, 2019 at 23:51
  • @lawlist the undo history is kept (using emacs undo - no wrappers)
    – ideasman42
    Oct 17, 2019 at 23:52

1 Answer 1

2

The revert-buffer function takes a few parameters which I think make the reload faster. I've got this bound to a keyboard shortcut for reloading the current buffer, it feels as fast as when you first open the file:

(defun reload-file-preserve-point ()
  (interactive)
  (when (or (not (buffer-modified-p))
            (y-or-n-p "Reverting will discard changes. Proceed?"))
    (save-excursion
      (revert-buffer t t t))
    (setq buffer-undo-list nil)
    (message "Buffer reverted")))
1
  • 5
    Welcome to emacs.SE and thank you for your answer! Maybe you could briefly describe what the args do that make the revert faster (this is something I immediately think I want to know when I read the answer).
    – JeanPierre
    Oct 18, 2019 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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