RevertBuffer

Is there a quick way of re-visiting the file in current buffer? This is useful when it was modified by an external program.

 From: MichaelSlassto
 Subject: Re: revisit-file
 Newsgroups: comp.emacs
 Date: Tue, 22 May 2001 20:13:45 GMT
  1. M-x revert-buffer RET yes RET
  2. C-x C-v RET
  3. (global-set-key [(control c) r] 'revert-buffer)
  4. C-x RET r RET RET

The second one is a sleazy trick, calling find-alternate-file - that prompts you for a file name, but fills in the name of the current buffer’s filename as a default, so you can hit RET to re-read the file from disk. Before I [who?] knew about find-alternate-file, I [who?] wanted something faster for reverting buffers (mainly due to the problem XEmacs has with hard links; see the WishList). That’s why I [who?] bound revert-buffer into the C-c keymap.

Actually, ‘C-x C-v’ is not a sleazy trick. And it’s not the same as ‘revert-buffer’. To see this, in DiredMode, mark some files, flag some for deletion, or whatever. Try ‘M-x revert-buffer’ or ‘g’ — no change. Now try ‘C-x C-v RET’. ‘C-x C-v’ is quite useful independently from ‘revert-buffer’, for this reason. – DrewAdams

The fourth one runs ‘revert-buffer-with-coding-system’ and prompt you for a new coding system (first ‘RET’). Also if a more recent autosave file is available, it will select it by default in the second ‘RET’ .. or enter ‘yes RET’ if you don’t have ido-yes-no (not necessarily what you want). – JohnDoe974

In addition to using invoking ‘revert-buffer’ manually, you can tell Emacs to do it automatically by putting this in your init file (see AutoRevertMode):

 (global-auto-revert-mode 1)
We have found that an awful lot of our network traffic can be people visiting multiple files across the network and having ‘global-auto-revert-mode’ on so no longer use it all the time – RaymondZeitler, comp.emacs, 24 May 2001

No confirmation

In misc-cmds.el you will find this extremely complex command definition:

  (defun revert-buffer-no-confirm ()
    "Revert buffer without confirmation."
    (interactive) (revert-buffer t t))

I bind it to `<F5>’ (since that’s the MS Windows key for syncing things). – DrewAdams

I am using that command, bind to `<f5>` too. It also check if the buffer has been modified before reverting it. You must provide the universal parameter in order to force the reload.

 (global-set-key
  (kbd "<f5>")
  (lambda (&optional force-reverting)
    "Interactive call to revert-buffer. Ignoring the auto-save
 file and not requesting for confirmation. When the current buffer
 is modified, the command refuses to revert it, unless you specify
 the optional argument: force-reverting to true."
    (interactive "P")
    ;;(message "force-reverting value is %s" force-reverting)
    (if (or force-reverting (not (buffer-modified-p)))
        (revert-buffer :ignore-auto :noconfirm)
      (error "The buffer has been modified"))))

YvesBaumes

Revert all buffers that are visiting a file

This is a little function that is useful when editing several files that might for any reason change on disk (update from VersionControl). It reverts all buffers that are visiting a file.

  (defun revert-all-buffers ()
    "Refreshes all open buffers from their respective files."
    (interactive)
    (dolist (buf (buffer-list))
      (with-current-buffer buf
        (when (and (buffer-file-name) (file-exists-p (buffer-file-name)) (not (buffer-modified-p)))
          (revert-buffer t t t) )))
    (message "Refreshed open files.") )

Here is a version Chris Stuart made for use after checking out a new branch on Git. It skips over any buffers with an asterisk in the name, so that you could add any other regex requirements to make a custom rule for which buffers to revert. If a file has been removed when moving to the new branch, it prompts whether to close the buffer.

  (defun revert-all-buffers ()
   "Iterate through the list of buffers and revert them, e.g. after a
    new branch has been checked out."
    (interactive)
    (when (yes-or-no-p "Are you sure - any changes in open buffers will be lost! ")
      (let ((frm1 (selected-frame)))
        (make-frame)
        (let ((frm2 (next-frame frm1)))
          (select-frame frm2)
          (make-frame-invisible)
          (dolist (x (buffer-list))
            (let ((test-buffer (buffer-name x)))
              (when (not (string-match "\*" test-buffer))
                (when (not (file-exists-p (buffer-file-name x)))
                  (select-frame frm1)
                  (when (yes-or-no-p (concat "File no longer exists (" (buffer-name x) "). Close buffer? "))
                    (kill-buffer (buffer-name x)))
                  (select-frame frm2))
                (when (file-exists-p (buffer-file-name x))
                  (switch-to-buffer (buffer-name x))
                  (revert-buffer t t t)))))
          (select-frame frm1)
          (delete-frame frm2)))))

Reverting but keeping undo

Current Emacs (since around 24.4) already does this, but older releases don't.

This fixes that.

(defun revert-buffer-keep-undo (&rest -)
  "Revert buffer but keep undo history."
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)
    (insert-file-contents (buffer-file-name))
    (set-visited-file-modtime (visited-file-modtime))
    (set-buffer-modified-p nil)))

Install in command ‘revert-buffer’ with

  (setq revert-buffer-function 'revert-buffer-keep-undo)

Reverting with undo

Until Emacs 24.4, there is no way to save the undo history if the file is changed behind Emacs’s back. Emacs needs to re-read the file from disk and reverting a buffer with ‘revert-buffer’ or reopening the file with ‘C-x C-v RET’ will lose the buffer’s undo history. If you only want to revert the buffer to undo just your own changes, you can hit the UndoCommands repeatedly until the buffer is no longer modified. Sometimes this is too onerous. The following command will run undo iteratively in the buffer for you and as far as possible or until the last time the file was saved.

(defun undo-until-reverted ()
  "Undo all edits until the buffer is unmodified."
  (interactive)
  (when (buffer-modified-p)
    (undo))
  (while (buffer-modified-p)
    (undo-more 1))
  (message "Buffer was undone until no longer modified"))

It can undo changes to a buffer, whether or not it is associated with a file.

A couple of bug reports exist: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18 and http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8447


CategoryFiles CategoryUndo