I currently have

(add-hook 'after-save-hook

in my config to automatically make scripts executable when saving them. However, I personally don't like that this means that also the autosaved files ending in ~, e.g., myscript.sh~, will automatically be rendered executable.

So my question is:

How can I automatically make only files not ending in ~ executable upon save? I guess one has to write a small wrapper function, but my elisp skills are quite under-developed ... Any help is appreciated!

  • 3
    Your myscript.sh would already have been converted to an executable using executable-make-buffer-file-executable-if-script-p. So the created backup file myscript.sh~ would also be an executable. executable-make-buffer-file-executable-if-script-p has nothing to do with it. May 6 '15 at 17:10
  • As an example, try the following: (1) touch foo (2) chmod 744 foo (3) Edit that file in emacs and save it (4) You will see a foo~ created which is also an executable, because the original file was an executable. May 6 '15 at 17:12

Wrap it with backup-file-name-p

(defun my-maybe-make-executable ()
  "Maybe make file executable unless it is a backup file."
  (unless (backup-file-name-p buffer-file-name)

The issue pointed out by @Kaushalmodi seems like a deal breaker though, perhaps a better solution is to specify a separate backup file directory. And remove executable privileges from the whole backup directory when backups occur.

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.