December 29, 2006

Emacs tips #3 : automatically making scripts executable

Posted in Emacs at 15:01 by theBlackDragon

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s