December 29, 2006
Emacs tips #3 : automatically making scripts executable
I don’t remember where I found this snippet (maybe comp.emacs) but it’s mighty useful so I thought I’d share it with you:
; Sets +x on scripts stating with a shebang (if (< emacs-major-version 22) (add-hook 'after-save-hook '(lambda () (progn (and (save-excursion (save-restriction (widen) (goto-char (point-min)) (save-match-data (looking-at "^#!")))) (shell-command (concat "chmod u+x " buffer-file-name)) (message (concat "Saved as script: " buffer-file-name)))))) (add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p))
Emacs versions below 22 need the former, longer method while Emacs 22 can use the oneliner in the last line.