Tim
Tim

Reputation: 2843

Get emacs to recognize python3 shebang

I'm using emacs to edit some Python 3 code, but it doesn't provide syntax highlighting when the shebang is #! /usr/bin/env python3. Highlighting works fine with just #! /usr/bin/env python. How do I get emacs to recognize a python3 shebang as a Python file, and provide appropriate syntax highlighting?

Edit: I'm using version 22.1.1, with no ability to change it.

Upvotes: 1

Views: 345

Answers (2)

Anton
Anton

Reputation: 1538

I came across the same problem you had here and the other answer by Rorschach did not work for me, also because I had an old version (24.3) of emacs which I couldn't upgrade. After trial and error, this worked for me:

Add to your .emacs file the line:

(push '("python3" . python-mode) interpreter-mode-alist)

Old emacs (prior to 24.4) did not support regex for editing interpreter-mode-alist, which was why the other answer's suggested fix did not work.

The changelog for emacs 24.4 mentions the new support for regex: "The cars (sic) of the elements in interpreter-mode-alist are now treated as regexps rather than literal strings."

Upvotes: 2

Rorschach
Rorschach

Reputation: 32426

Check the value of auto-mode-interpreter-regexp, which should match the shebang entry correctly by default. Then, ensure there is an entry in your interpreter-mode-alist like

("python[0-9.]*" . python-mode)

If not for some reason, add it in your init file, eg.

(cl-pushnew '("python[0-9.]*" . python-mode) interpreter-mode-alist :test #'equal)

Edit

Since your emacs is quite ancient, try

(push '("python[0-9.]*" . python-mode) interpreter-mode-alist)

Upvotes: 1

Related Questions