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
[...]
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

  • 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 1:03
  • See my updated answer, with an implementation of your request – matteol Jun 22 '18 at 6: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

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.