3

I'm experiencing frustrating behaviour with automatic Python indentation when ending a highly indented block. I've used pilcrows to indicate where I want newlines in the following example:

def f():
    if True:
        if True:
            if True:
                print ""
¶
¶
¶
def g():
    print ""

After finishing typing the first function, every subsequent line is automatically indented by 16 spaces, which all need to be deleted. I understand that the first time I hit return, indenting to that level makes sense. But I am surprised that when hitting return on an unindented line it would also be assumed that I wanted the maximum valid indentation.

This behaviour seems to occur in 24.4.1 with both the new python.el (Gallina) and python-mode.el. (It also appeared to happen when I tried to use the old python.el by Dave Love, but I could not work out how to verify which python.el had actually loaded). I note that with the new python.el I can use C-j to return without indenting, but that doesn't help much with a situation such as the following:

def f():
    if True:
        if True:
            if True:
                print ""
    ¶
    print ""

I don't want to disable automatic indentation, but is there a way to make it less aggressive and accept valid lower levels of indentation somehow?

1 Answer 1

2

With python-mode.el each block may have it's own length of indent-step, i.e. the value of py-indent-offset.

py-smart-indentation must be on.

When introducing a new block, the desired indentation needs to be set/correct on the first indented line. With the next line, the new distance should have been learned.

In case of error, please file a bug-report here:

https://bugs.launchpad.net/python-mode

1
  • This works in the Emacs GUI, but it's not working for me when running in the terminal with emacs --no-window-system. I shall investigate further and file a bug if necessary. Commented Jun 12, 2015 at 14:35

Your Answer

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

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