In general I want Emacs to save a backup file for me, except in some special directories.

I tried to define a Directory Local Variables, and add:

((nil . ((make-backup-files . nil))))

But it seem it won't take effect. I think it is because make-backup-files is a global variable.

Is there a equivalent local variable to control the backup creation function?


1 Answer 1


Backup decisions seem to pass through the function normal-backup-enable-predicate, via the variable backup-enable-predicate which is set to this by default. You can provide your own function to return nil if no backup is to be done. For example,

(defun my-backup-enable-predicate (name)
  (let (found)
    (dolist (specialdir '("/somedir/" "/some/other/") found)
      (if (string-prefix-p specialdir name)
          (setq found t)))
    (if found
      (normal-backup-enable-predicate name))))

(setq backup-enable-predicate #'my-backup-enable-predicate)

This tests if the provided full pathname begins with one of the listed directories (/somedir/ or /some/other/ in this example) and if so returns nil, otherwise calls the usual backup test function. The final line make emacs use your function. To go back to the real function use

(setq backup-enable-predicate #'normal-backup-enable-predicate)

I reread your question and realised I hadn't answered it. To allow you to set make-backup-files in your .dir-locals.el I think all you need is to make it buffer local in your startup file:

(make-variable-buffer-local 'make-backup-files)

The documentation says "Buffer-local bindings are normally cleared while setting up a new major mode, unless they have a 'permanent-local' property.". You can set this property with

(put 'make-backup-files 'permanent-local 't)
  • This is not what I want...but seems to be the closest I could get. Thx~
    – David S.
    Commented May 16, 2016 at 12:31
  • @davidshen84 Sorry, I think I had forgotten the important part of your question when I got round to my answer. I think there is a much simpler solution available for you, see updated answer.
    – meuh
    Commented May 16, 2016 at 14:25
  • I found make-variable-buffer-local do not work for me. I have to use make-local-variable. Any idea?
    – David S.
    Commented May 24, 2016 at 6:29
  • @davidshen84 Perhaps the major-mode is clearing the buffer-local binding, as mentioned in the doc. See the new last line in my answer for how to set a property to avoid this (in your startup file).
    – meuh
    Commented May 24, 2016 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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