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. Commented May 6, 2015 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. Commented May 6, 2015 at 17:12

2 Answers 2


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.


I have the same issue, in that I don't like myscript.sh myscript.sh~ in my completion list. This is my lazy solution, there is a better way of setting a better permission value, clearly this grants more than it should at times.

;; chain a few commands for ashook
(defun savehookactions()
    (set-file-modes (make-backup-file-name buffer-file-name) #o644)
;; make script executable after save
;;(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
(add-hook 'after-save-hook 'savehookactions)

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.