3

If I edit an executable script, say foo.sh, then upon saving emacs makes the backup file foo.sh~. That's all well and good, but if the original file has the executable bit set, then so does the backup. This is a little annoying; is there a way to prevent this?

Or, at the very least, is there a way to save the executables in a different directory than the real one? That way I could put them somewhere not on PATH. Thanks!


$ emacs --version
GNU Emacs 25.3.1
[...]
greatBigDot
  • 153
  • 3

1 Answers1

1

The variable backup-directory-alist tells emacs where to save the backups. Adding the following to your init file will set its value only if the executable bit is set.

(defun make-executable-backup-file-name (file)
  "docstring"
  (if (file-executable-p file)
      (let ((backup-directory-alist '(("." . "~/.emacs.d/executable-backup"))))
        (make-backup-file-name--default-function file))
    (make-backup-file-name--default-function file)))

(setq make-backup-file-name-function #'make-executable-backup-file-name)

Writing a proper docstring and a better name for the function is left as an exercise for the reader

matteol
  • 1,868
  • 1
  • 9
  • 11
  • Thanks. Can I set it up so that backups are only saved in a different place only when the file's executable bit is set? (In general I like having the backup be in the same directory; it's just when the file is executable that gets annoying.) – greatBigDot Jun 22 '18 at 01:03
  • See my updated answer, with an implementation of your request – matteol Jun 22 '18 at 06:46
  • Thanks! In lieu of a way to prevent the bit from being set on backups, this is what I need. – greatBigDot Jun 24 '18 at 16:43