1

I'm using emacs 24.3 and Ubuntu 12.04 LTS. How do I make emacs automatically indent lines in Python, like in IDLE? Currently, it does not do that.

Also, in general, how would I do this for any programming language, say, Java or c++?

4
  • jesshamrick.com/2012/09/18/emacs-as-a-python-ide Commented Apr 10, 2014 at 16:08
  • Does that still work with emacs 24? Commented Apr 10, 2014 at 18:54
  • give it a try, I don't use it to be honest. Commented Apr 10, 2014 at 19:49
  • I just tried the stuff in there, but emacs is still not auto-indenting. Commented Apr 10, 2014 at 23:01

3 Answers 3

4

Try electric-indent-mode. It will be ebabled by default in Emacs-24.4. But note that the version in 24.3 probably doesn't work too well in python-mode buffers.

Sign up to request clarification or add additional context in comments.

3 Comments

Add (electric-indent-mode 1) in your ~/.emacs file.
I tried doing that, but now when I un-indent a line, and press the return key, the previous line indents back to its original position.
Right: "the version in 24.3 probably doesn't work too well in python-mode".
1

You can try other python modes, like emacs-for-python or elpy, as given by this doc: http://wikemacs.org/index.php/Python (though the default mode works fine for me on the same config).

You can install them with package.el (M-x package-install RET elpy RET) and call them as explained.

3 Comments

I used the emacs-for-python, and I love it! Thank you so much!
One more question, actually: how would I do this for any other language, like java?
Glad you liked it ! Mmmh… if your mode doesn't already do it, you may remap the RET key to a new-line (newline) and indent. This combo is currently done for me with reindent-then-newline-and-indent. You can investigate with the help (see C-h ?).
1

Open a newline with C-j, you should get the indentation.

3 Comments

How about auto-identation by just pressing enter?
The result depends from kind of python-mode resp. customization. With python-mode.el, that's the default, yes.
I love C-j, because that frees up the tab key for GitHub Copilot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.