9

That is, can I specify for a particular file that I don't want Emacs to notify me that the file has changed on disk and prompt me about whether I want to accept changes or not? I don't want to do this globally, just for files I know will frequently be changed outside of Emacs (in particular, calendar-related Org files).

8

You can do this for a specific buffer with M-x auto-revert-mode (manual). For file buffers, this will check every auto-revert-interval seconds (defaults to 5) for changes and update the buffer from disk automatically.

Your title says "a particular file" while your question's body mentions a file type (org files). Depending on your circumstances, here are a few ways you might set this mode to get the desired effect in a persistent manner. The earlier methods are arguably cleaner and more efficient if they apply to your particular use case.

If you know you want this for every file of a certain type, consider adding a mode hook to your init setup.

(add-hook 'org-mode-hook (lambda () (auto-revert-mode 1)))

If you know you want it for everything in a certain directory, consider setting it in .dir-locals.el in the relevant location, making sure to VCS-ignore the local settings file if appropriate. This example will apply to all org-mode buffers for files under the same directory (and subdirs). You can also run M-x add-dir-local-variable to have Emacs generate and save this for you automatically.

(
  (org-mode . ((auto-revert-mode . 1)))
 )

If neither of the two approaches above works well for you, consider a file local variable set in each file. The shortest, most general way to set one of these is to include the following in the first line of the target file:

-*- eval: (auto-revert-mode 1); -*-

In source files, the text can follow a comment delimiter (//, #, etc). You can run M-x add-file-local-variable-propline to have Emacs generate this for you automatically, or M-x add-file-local-variable for a format that uses the bottom of your file instead of the top.

Use this method only if you're not sharing the file with anyone else. From the GNU manual:

It is often a mistake to specify minor modes this way. Minor modes represent individual user preferences, and it may be inappropriate to impose your preferences on another user who might edit the file. If you wish to automatically enable or disable a minor mode in a situation-dependent way, it is often better to do it in a major mode hook.

  • 2
    Isn't (auto-revert-mode 1) a canonical way to turn a minor mode on instead of setq? – mbork Nov 16 '14 at 19:01
  • 1
    Yes, you're right. Cleaning that example up in one second. Thanks. – Mian Nov 16 '14 at 19:03
  • 1
    You're welcome. (That said, AFAIU, file local variables are best suited for OP's use case.) – mbork Nov 16 '14 at 19:16
  • 1
    You may be right. IMO it depends on the details of their file organization. If the files they know will be frequently changed outside of emacs are all org files, or all happen to be in the same place (e.g. a Dropbox directory), a mode- or directory-based approach could be cleaner and easier, and will avoid cluttering their files as a bonus. – Mian Nov 16 '14 at 19:21
  • 2
    To interface with both of these methods, there are add-file-local-variable and add-dir-local-variable. – Sean Allred Nov 17 '14 at 13:32
3

Yes. Put this at the end of your file.

* COMMENT Config
Local Variables:
eval: (auto-revert-mode 1)
End:

This assumes Org-mode, for other modes use comments in the language used as explained in the link given below.

Note: see Local Variables in Files in the manual for the background, safety information and a caveat that this might not always be a good idea. Also, the section on reverting says e.g. how to change the default, 5-secind interval for auto-reverting.

  • Does this absolutely need to be at the end? I have a script which is generating .org files and I had it put it in near the beginning -- which doesn't seem to work.... – emacsomancer Nov 19 '14 at 22:21
  • 1
    Yes, see the link. And see other answers for a first-line solution. – mbork Nov 20 '14 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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