I usually work on files which are updated in the file system via version control. What's a quick way to reload a file without having to C-x C-f the file again and getting asked if I want to reload it?

up vote 85 down vote accepted

M-x revert-buffer will do exactly what you want. It will still ask for confirmation.

Another option (my favorite) is the below function:

;; Source: http://www.emacswiki.org/emacs-en/download/misc-cmds.el
(defun revert-buffer-no-confirm ()
    "Revert buffer without confirmation."
    (interactive)
    (revert-buffer :ignore-auto :noconfirm))
  • 7
    You might have cited the library that that code is from: misc-cmds.el. Not that it is complicated, but when you copy something exactly it is common courtesy to point to the source. – Drew Sep 24 '14 at 15:11
  • 3
    Apologies! I, myself, wasn't sure where I got that snippet from. I should have at least googled the function name and try to find its source so that I can credit it here. Update I have noted down the sources for everything I copied; just wasn't sure about this one: github.com/kaushalmodi/.emacs.d/blob/master/setup-files/… – Kaushal Modi Sep 24 '14 at 15:12
  • 4
    I wouldn't recommend this function. It's too dangerous to use accidentally on a modified file. – Gilles Sep 27 '14 at 0:13
  • 2
    @Gilles I agree, with great power comes great responsibility. I wouldn't bind that to an easy to access binding. – Kaushal Modi Sep 27 '14 at 0:44
  • 5
    My variant of this will prompt for confirmation only if the buffer is modified, using (revert-buffer t (not (buffer-modified-p)) t). – glucas Jul 10 '15 at 15:23

There is also auto-revert-mode which does it automatically and gives you feedback.

From the doc string:

auto-revert-mode is an interactive autoloaded compiled Lisp function
in `autorevert.el'.

(auto-revert-mode &optional ARG)

Toggle reverting buffer when the file changes (Auto Revert mode).
With a prefix argument ARG, enable Auto Revert mode if ARG is
positive, and disable it otherwise.  If called from Lisp, enable
the mode if ARG is omitted or nil.

Auto Revert mode is a minor mode that affects only the current
buffer.  When enabled, it reverts the buffer when the file on
disk changes.

Use `global-auto-revert-mode' to automatically revert all buffers.
Use `auto-revert-tail-mode' if you know that the file will only grow
without being changed in the part that is already in the buffer.

Another option, which I use, is find-alternate-file bound to C-x C-v. This opens a file reusing your current buffer.

By default, it points to the file you're currently on, so you can just type C-x C-v RET to reload your file. It won't prompt unless your buffer has unsaved data.

Some non-text modes like image-mode (used for rendering pictures, pdfs, svgs...etc) and dired have revert-buffer bound to g for faster access.

  • This is great, thanks! – DilithiumMatrix Apr 17 '15 at 14:44
  • 2
    This is my new go to. Thanks for mentioning it. – ramsey_tm Nov 10 '16 at 15:54
  • I had a (ffap-bindings) in my .emacs which seems to change the binding of C-x C-v. – ychaouche Jan 10 at 14:34

Emacs calls this reverting.

You can revert the current file with M-x revert-buffer. This prompts for confirmation whether the file has been modified or not, except for files that match patterns listed in the variable revert-without-query (see the manual for details). Another occasional annoyance of revert-buffer is that it resets the file mode to the default.

I use the following function to revert a bunch of files, given by name. If a file isn't opened in some buffer, it is ignored.

(defun revert-files (&rest files)
  "Reload all specified files from disk.
Only files that are currently visited in some buffer are reverted.
Do not ask confirmation unless the buffer is modified."
  (save-excursion
    (let ((revert-without-query '("")))
      (dolist (file-name files)
        (message "Considering whether to revert file %s" file-name)
        (let ((buf (find-buffer-visiting file-name)))
          (when buf
            (message "Reverting file in buffer %s" (buffer-name buf))
            (set-buffer buf)
        (revert-buffer t nil t)))))))

A typical use case for this function is after updating files from version control. Use emacsclient to call revert-files on all the files that have been updated, or (this is easier, and only slightly slower) on all the files concerned by the update. I call the following shell script, passing it the files as arguments:

#! /bin/sh
# Similar to gnuclient, but call `revert-files' on the files.
files=

## Find a way to convert a path to absolute. Bizarre OSes such as Windows
## require special cases. We also try to detect non-filenames such as URIs.
case `uname -s` in
  CYGWIN*)
    absolute_path () {
      cygpath -a -w -- "$1"
    };;
  *)
    wd="`pwd -P 2>/dev/null || pwd`"
    absolute_path () {
      case "$1" in
        /*) printf '%s' "$1";; # ordinary absolute path
        *:/*)
          if expr "z$1" : 'z[0-9A-Z_a-z][-.0-9@A-Z_a-z]*:/.*'; then
            printf '%s' "$1" # URI or machine:/some/path
          else
            printf '%s' "$wd/$1" # default to a relative path
          fi;;
        *) printf '%s' "$wd/$1";; # default to a relative path
      esac
    };;
esac

for x; do
  files="$files \"`absolute_path "$x" | sed 's/[\\\\\\\"]/\\\\&/g'`\""
done
exec emacsclient -e "(revert-files$files)"

Usage example:

svn update
find -name .svn -prune -o -type f -exec emacsclient-revert {} +

revert-buffer is there. But I like to have some feedback.

I have the following in my .emacs.

(global-set-key (kbd "C-c r") (lambda ()
                                (interactive)
                                (revert-buffer t t t)
                                (message "buffer is reverted")))

you can also enable global-auto-revert-mode as shown below

(global-auto-revert-mode 1)

this is helpful when you do a lot of checks of your js files with auto-fix mode enabled, like in jssc.

Magit manages file reversions for you automatically, thus solving your core problem. You also benefit from its other features.

Here are the docs for the tweaking the settings you're interested in:

If you stick with Magit, also be sure to enable all 3 global WIP modes (Work In Progress) to avoid losing work.

You can thus perform version-control actions inside Emacs with Magit and avoid your original problem altogether.

  • 1
    Please let me know if this is helpful. – webappzero Aug 14 '17 at 23:33
  • Yes. Thank you. – Drew Aug 15 '17 at 0:05

You can use find-alternate-file, which is bound to C-x C-v by default, and just simply type RET at the prompt to reload the file.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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