0

I start up my Emacs25 with emacs25 -Q to skip any init.el. When I then open a python file with local file variables as shown below, TAB indents only two spaces, instead of four:

# -*- mode: Python; python-indent-offset: 4; python-guess-indent: nil

for i in range(0, 10):
  print i

def f( i ):
  print i

What do I need to do, to get python-mode to honour the python-indent-offset?

  • Works for me, I get 4 spaces with that setup. – npostavs Jan 22 '18 at 12:30
  • @npostavs try it again with my example. python-mode seems to be guessing the indentation. However, I asked python-guess-indent to be nil. – Arne Jan 22 '18 at 15:24
  • Furthermore, I just also tried python-indent-guess-indent-offset: nil, since python-guess-indent is deprecated. Still no luck. – Arne Jan 22 '18 at 15:25
  • Oh, seems to work if I add the missing -*- at the end of first line. – npostavs Jan 22 '18 at 23:50
  • @npostavs Thanks! I was blind! Care to put it in an answer? – Arne Jan 23 '18 at 10:35
3

The syntax for file-local variables requires -*- on both ends (OP is missing the closing one); the following works correctly:

# -*- mode: Python; python-indent-offset: 4; python-guess-indent: nil -*-

for i in range(0, 10):
  print i

def f( i ):
  print i

Also of interest may be the commands add-file-local-variable (adds the variable settings to the end of the file) and add-file-local-variable-prop-line (adds the setting to the top line, thanks to Basil for pointing it out).

  • 2
    There is also add-file-local-variable-prop-line for this purpose. – Basil Jan 23 '18 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.