197

I have a non-emacs global search and replace function that causes my disk files to become more up-to-date than my emacs buffers (en masse). Is there any way to tell emacs to refresh all the buffers from disk in one fell swoop, instead of having to do each one individually by reloading the file?

Thanks! D

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Dave
  • 2,653
  • 4
  • 20
  • 22

3 Answers3

254

(global-auto-revert-mode t) in your .emacs.

Ashwin
  • 3,609
  • 2
  • 18
  • 11
  • 1
    This looks good in cases where I always want to stay synched, thank you! I think I'll end up using revbuffs so that I can manage conflicts myself ( otherwise sometimes I might lose unsaved changes, the way my workflow currently works. ) – Dave Sep 27 '09 at 00:53
  • 2
    Thank you! I'm using a combination of revbuffs and auto-revert-mode. auto-revert-mode works great when I want to overwrite changes. – Natan Yellin Jun 16 '11 at 10:04
  • 14
    @Dave: (global-)auto-reverse-mode will not revert a file if the corresponding buffer is modified, so there should be no risk of you losing unsaved changes. – Lindydancer Mar 04 '12 at 15:23
  • I have set the variable in .emacs, but when i edit some code xcode, then read the file(already open) at that line, edit in xcode not there, need M-x revert-buffer handly.(mac + emacs 24.3.1) – ericfang May 07 '13 at 03:38
  • 7
    Works nicely when switching branches with git. Thanks! – Justin Leitgeb Nov 02 '13 at 22:19
  • Is it possible to make it refresh more frequently? Perhaps even instantaneously without polling using some platform specific notification mechanism? – Erik Kaplun Jan 06 '14 at 18:29
13

Here is an alternative if you are using Emacs GUI (Mine is GNU Emacs 25.1.1 on Windows 7):

  1. Click "Options" in menubar
  2. Select "Customize Emacs"
  3. Select "Saved Options"
  4. Then you should see a search field where you enter "global-auto-revert-mode" and press "Search" button
  5. Click "Toggle" button and make sure it reads "on" to the right of the button
  6. Press "Apply and Save" button right below the search field

If you already have a few files opened, you should see this taken effect instantly. Good luck!

benjaminz
  • 3,118
  • 3
  • 35
  • 47
11

Thanks to @Ashwin for pointing out (global-auto-revert-mode t). I found that Emacs won't automatically update buffers whose files have changed on disk with this change alone. From https://www.gnu.org/software/emacs/manual/html_node/emacs/Reverting.html:

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.

After I set (setq auto-revert-use-notify nil), Emacs did indeed refresh all buffers every 5 seconds.

jdc
  • 680
  • 1
  • 8
  • 11