normal-mode
(cited by @Omar in the alternative answer) calls, in part, set-auto-mode
. The non-interactive function set-auto-mode
, which can be evaluated by typing M-:
and then (set-auto-mode)
, or by typing M-x eval-expression RET (set-auto-mode) RET
, places the buffer in the proper major-mode
based upon the shebang line provided that said line matches the expected format ... (infra). normal-mode
; however, is interactive and contains some additional goodies such as killing all local variables, running certain hooks, etc.
The variable auto-mode-interpreter-regexp
, defined in files.el
has a default value of:
"#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)"
We note that there is no space between the #
and the !
symbol, whereas the O.P. is using a space between the aforementioned symbols; i.e., # !/usr/bin/env python
. Emacs is unable to match the shebang line of the O.P. against the default regexp because of the aforementioned unexpected space between the #
and !
symbols at the outset of said shebang line.
The doc-string for the above-mentioned variable provides as follows:
Regexp matching interpreters, for file mode determination.
This regular expression is matched against the first line of a file
to determine the file’s mode in ‘set-auto-mode’. If it matches, the file
is assumed to be interpreted by the interpreter matched by the second group
of the regular expression. The mode is then determined as the mode
associated with that interpreter in `interpreter-mode-alist`.