I forget to enter sh-mode
when I create new shell scripts in ~/.local/bin
, so I want emacs to do this automatically. Because I have an advice set up to make them executable, this will have the effect of also marking them executable automatically.
I tried creating this .dir-locals.el
file in the directory:
((nil . ((magic-fallback-mode-alist . (("^$" . sh-mode))))))
but although it asked me whether to apply the "risky" file-local variable magic-fallback-mode-alist
, it did not work - the new buffer was still using fundamental-mode.
sh-mode
inauto-mode-alist
. For example:(add-to-list 'auto-mode-alist '("\\.sh\\'" . sh-mode))
. This should already be the default, but if you are using another extension, you can set it this way (this will apply globally though, not just to files in that directory.normal-mode
calls(set-auto-mode)
and(hack-local-variables)
in that order (as indeed it needs to in order for directory local specifications to match on the major mode). This is why your.dir-locals.el
does not have the desired effect.(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
, any file that begins with#!
, example#!/bin/sh
, will be automatically made an executable when you save the file. Adding that hash-bang will also automatically set the correct sh-mode (correct flavor of sh).