0

If I had a python file that I wanted emacs to open in python mode, what would I put at the top of that .py file?

For example, if it was an .h file that I wanted emacs to open in c++ mode, I would put:

//===-------------------------------*- C++ -*-===//

What is the equivalent for python?

2 Answers 2

3

If you have a .py file, Emacs should already know to open it with the Python major mode. Otherwise, see this.

If for any reason you need to be explicit about it, you can specify file variables at the top of your file in a comment; e.g.:

# -*- mode: python -*-
Sign up to request clarification or add additional context in comments.

Comments

1

If the file name already ends with .py, you shouldn't have to do anything. Emacs should pick the right mode based on the extension. Or if the file starts with a #! interpreter specification for python, Emacs should pick the right mode based on that.

Anyway, if you want to set the mode explicitly, add a comment containing -*- mode: python -*- on the first line of the file — or if the first line specifies a Python interpreter, you can put the comment on the second line instead.

So your file would start with:

#!/usr/bin/env python
# -*- mode: python -*-

You can find more information and examples in the Specifying File Variables section of the Emacs manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.