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
M-x revert-buffer RET yes RET
C-x C-v RET
(global-set-key [(control c) r] 'revert-buffer)
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 knew about find-alternate-file, I wanted something faster for reverting buffers (mainly due to the problem XEmacs has with hard links; see the WishList). That’s why I bound revert-buffer into the C-c keymap.
‘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
In addition to [using
‘M-x revert-buffer’]…, you can tell Emacs to do it automatically by putting this in your .emacs file:
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
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
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) (not (buffer-modified-p))) (revert-buffer t t t) ))) (message "Refreshed open files.") )
There’s 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.
Emacs doesn’t actually save undo history with
‘revert-buffer’ see http://lists.gnu.org/archive/html/bug-gnu-emacs/2011-04/msg00151.html
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
(setq revert-buffer-function 'revert-buffer-keep-undo)