Question

I have defined a python mode I would like to use for all .py files. Including:

(add-to-list 'auto-mode-alist '("\\.py\\'" . my-python-mode))

correctly uses my mode in all files ending with .py unless that file begins with a

#! /usr/bin/env python

which several files I'm working with do. How can I make my-python-mode take preference to the python mode emacs is defaulting to?

Was it helpful?

Solution

From the emacs manual, you need to also update interpreter-mode-alist

It has the entry

("python" . python-mode)

Do the add-to -list here as well

Second, if there is no file variable specifying a major mode, Emacs checks whether the file's contents begin with ‘#!’. If so, that indicates that the file can serve as an executable shell command, which works by running an interpreter named on the file's first line (the rest of the file is used as input to the interpreter). Therefore, Emacs tries to use the interpreter name to choose a mode. For instance, a file that begins with ‘#!/usr/bin/perl’ is opened in Perl mode. The variable interpreter-mode-alist specifies the correspondence between interpreter program names and major modes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top