Frage

I just installed python-mode from ELPA and when I hit Enter the cursor goes straight to the beginning of the next line. For instance for the following code:

def hello_world():
    print "hello world!"
    for i in range(3):
        print "hello again"
    return 0

I have to hit TAB after Enter every time after I hit Enter to go to a new-line (although interestingly TAB sends the cursor to the right place).

When I installed python-mode I got some warning messages relating to indentation which I don't really understand, here is a link to those warning messages.

War es hilfreich?

Lösung

C-j is the key I was looking for ('newline-and-indent' command). I managed to rebind Enter to this by putting the following hook in my init.el:

(add-hook 'python-mode-hook
          (lambda ()
             (define-key python-mode-map "\r" 'newline-and-indent)))

Andere Tipps

Not sure why you installed python-mode, since Emacs comes with Python support built-in, but I don't think this affects the rest. If you want RET to indent after inserting a the newline, you probably like that everywhere, so you might like to enable electric-indent-mode.

Try using C-j instead of enter.

Actually, C-j is bound to (newline-and-indent), which does the two steps(enter and tab)mentioned in the question.

As of version 24.4.50.1 the default behavior has been reversed i.e enter adds newline and indent if needed while C-j doesn't. Although the functions these key-bindings map to aren't as mentioned above.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top