Search code examples
pythonemacspython-mode

python-mode indentation not working upon hitting enter


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.


Solution

  • 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)))