If you have made extensive changes to a file and then change your mind about them, you can get rid of them by reading in the previous version of the file. To do this, use M-x revert-buffer, which operates on the current buffer. Since reverting a buffer unintentionally could lose a lot of work, you must confirm this command with yes.
revert-buffer tries to position point in such a way that, if
the file was edited only slightly, you will be at approximately the
same piece of text after reverting as before. However, if you have made
drastic changes, point may wind up in a totally different piece of text.
Reverting marks the buffer as “not modified”.
Some kinds of buffers that are not associated with files, such as
Dired buffers, can also be reverted. For them, reverting means
recalculating their contents. Buffers created explicitly with
C-x b cannot be reverted; revert-buffer reports an error
if you try.
When you edit a file that changes automatically and frequently—for
example, a log of output from a process that continues to run—it may
be useful for Emacs to revert the file without querying you. To
request this behavior, set the variable revert-without-query to
a list of regular expressions. When a file name matches one of these
regular expressions, find-file and revert-buffer will
revert it automatically if it has changed—provided the buffer itself
is not modified. (If you have edited the text, it would be wrong to
discard your changes.)
In addition, you can tell Emacs to periodically revert a buffer by
typing M-x auto-revert-mode. This turns on Auto-Revert mode, a
minor mode that makes Emacs automatically revert the current buffer
every five seconds. You can change this interval through the variable
auto-revert-interval. Typing M-x global-auto-revert-mode
enables Global Auto-Revert mode, which does the same for all file
buffers. Auto-Revert mode and Global Auto-Revert modes do not check
or revert remote files, because that is usually too slow.
One use of Auto-Revert mode is to “tail” a file such as a system
log, so that changes made to that file by other programs are
continuously displayed. To do this, just move the point to the end of
the buffer, and it will stay there as the file contents change.
However, if you are sure that the file will only change by growing at
the end, use Auto-Revert Tail mode instead
(auto-revert-tail-mode). It is more efficient for this.
Auto-Revert Tail mode works also for remote files.
See VC Mode Line, for Auto Revert peculiarities in buffers that visit files under version control.
blog comments powered by Disqus