4

I've recently declared emacs bankruptcy and in rebuilding my config switched from the old python-mode.el to the built-in python.el.

One thing that's I'm missing is the old behaviour of auto-indenting to the correct level when hitting RET. Is there any way to re-enable this?

Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134

2 Answers2

4

In upcoming Emacs 24.4 auto-indendation is enabled by default thanks to electric-indent-mode. Since Emacs 24.4 has been in feature-freeze for quite some time now, there should be no major breaking bugs left, so you could already make a switch.

  • 1
    Indeed, and please note that in order to enable it by default, `electric-indent-mode` has seen some improvements: the version in 24.3 is unusable with python.el, whereas the version in 24.4 should work fine. – Stefan Mar 07 '14 at 18:29
3

Try this:

(add-hook 'python-mode-hook 'my-python-hook)

(defun my-python-hook ()
  (define-key python-mode-map (kbd "RET") 'newline-and-indent))
abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • 1
    I combined your answer with lunaryorn's as follows, `(add-hook 'python-mode-hook 'electric-indent-mode)`. – wsaleem Jul 04 '14 at 12:20
  • this change added indent when I hit but now it indents with several tabs (5 or 6) for new .py files. How can I set it to 4 white spances? – Usobi Nov 22 '14 at 23:26