Next: Autorevert, Previous: Saving, Up: Files
If you have made extensive changes to a file-visiting buffer and then change your mind, you can revert the changes and go back to the saved version of the file. To do this, type M-x revert-buffer. Since reverting unintentionally could lose a lot of work, Emacs asks for confirmation first.
The revert-buffer
command tries to position point in such a
way that, if the file was edited only slightly, you will be at
approximately the same part of the text as before. But if you have
made major changes, point may end up in a totally different location.
Reverting marks the buffer as not modified. However, it adds the reverted changes as a single modification to the buffer's undo history (see Undo). Thus, after reverting, you can type C-/ or its aliases to bring the reverted changes back, if you happen to change your mind.
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.)
You can also tell Emacs to revert buffers periodically. To do this
for a specific buffer, enable the minor mode Auto-Revert mode by
typing M-x auto-revert-mode. This automatically reverts the
current buffer when its visited file changes on disk. To do the same
for all file buffers, type M-x global-auto-revert-mode to enable
Global Auto-Revert mode. These minor modes do not check or revert
remote files, because that is usually too slow. This behavior can be
changed by setting the variable auto-revert-remote-files
to
non-nil
.
By default, Auto-Revert mode works using file notifications,
whereby changes in the filesystem are reported to Emacs by the OS.
You can disable use of file notifications by customizing the variable
auto-revert-use-notify
to a nil
value, then Emacs will
check for file changes by polling every five seconds. You can change
the polling interval through the variable auto-revert-interval
.
Not all systems support file notifications; where they are not
supported, auto-revert-use-notify
will be nil
by
default.
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.
When a buffer is auto-reverted, a message is generated. This can be
suppressed by setting auto-revert-verbose
to nil
.
In Dired buffers (see Dired), Auto-Revert mode refreshes the buffer when a file is created or deleted in the buffer's directory.
See VC Undo, for commands to revert to earlier versions of files under version control. See VC Mode Line, for Auto Revert peculiarities when visiting files under version control.