11

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

For example:

for i in range(10):
    print(i)
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?

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
Hunger
  • 213
  • 2
  • 7
  • Can you add your .emacs init file – Vivian Maya Nov 09 '14 at 16:00
  • 2
    @VivianMaya This has nothing to do with config files, `emacs -Q` still has this problem – Hunger Nov 09 '14 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. – Vivian Maya Nov 09 '14 at 16:21

1 Answers1

17

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))))
Stefan
  • 26,154
  • 3
  • 46
  • 84
Kyle Meyer
  • 6,914
  • 26
  • 22