I'm using Emacs 24.4. When writing Python code, Emacs behaves strangely with respect to indentation.

For example:

for i in range(10):
for i in range(10)

When I input :, Emacs adds a needless indent at the beginning of the third line. This is quite strange, is this a bug?

  • Can you add your .emacs init file Nov 9, 2014 at 16:00
  • 2
    @VivianMaya This has nothing to do with config files, emacs -Q still has this problem
    – Hunger
    Nov 9, 2014 at 16:04
  • my friend sometimes Indentation might cross something, I had problem like this and that was about crossing definition. If you sure its nothing to do with, then I clearly don't know what is going on. Nov 9, 2014 at 16:21

1 Answer 1


This is caused by electric-indent-mode, which is enabled by default in Emacs 24.4. You can turn it off with

(electric-indent-mode -1)

If you don't want to disable electric indent mode entirely but want to stop the colon from activating electric indent mode in Python mode, then you could remove the colon character from electric-indent-chars, which Python mode gives a local value.

(add-hook 'python-mode-hook
          (lambda ()
            (setq electric-indent-chars (delq ?: electric-indent-chars))))
  • What I'd really like to see is: when the indent I've put is legal Python, LEAVE IT ALONE. electric-indent-mode mindlessly guesses what I want when I've already told it...
    – AdamC
    Jun 14, 2019 at 16:53
  • 1
    And lo, these many years later, it's still broken.
    – AdamC
    Feb 18, 2020 at 18:03
  • And lo, two more years have passed.
    – Edman
    Sep 6, 2022 at 14:24

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.