I am using Emacs python-mode. I invoke it using this in my .emacs
(add-to-list 'load-path "~/emacs/python-mode.el-6.0.3/")
(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
(lambda ()
(set-variable 'py-indent-offset 4)
;(set-variable 'py-smart-indentation nil)
(set-variable 'indent-tabs-mode nil)
(define-key py-mode-map (kbd "RET") 'newline-and-indent)
;(define-key py-mode-map [tab] 'yas/expand)
;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
))
It generally indents okay, in that if I wrote:
if condition:
and then pressed Return, it would properly put the cursor in the indented newline. The problem is that it doesn't exit out of indentation correctly. In other systems, when I make a new line in the body of the indented clause (like the if
statement body) and press Backspace, it jumps one level out in indentation, instead of backspacing. For example, if I had:
if condition:
statement1
statement2
and I pressed return, backspace, after statement2
, it would put the cursor here:
if condition:
statement1
statement2
<-- cursor position
If you have many idented levels and it doesn't do this, editing Python becomes impossible, since you have to backspace manually until you get to the right indentation level... it's error prone and annoying, e.g. if you had:
for something:
for other:
if hello:
while x:
statement1
<-- How to indent back to level of "for other"?
EDIT: I was unable to get this to work while running emacs as "emacs -nw" (I am logging in to a server remotely and don't want the X interface to start). When I remove "-nw" and use emacs, with the slower X interface remotely, it all works... any idea why this might be? Could it be a shell configuration issue related to backspaces or something like that?
How can this be fixed? I just want it to backspace at indent levels if I'm inside a clause. thanks.
For modern versions of Emacs, use python.el, not python-mode.el. If you remove all those lines from your .emacs, I think python.el will be enabled by default, and you'll get a Python mode when you open a .py file.
I tested the blank default setting on my system and backspace properly goes back one level of indention in your example.
So the answer seems to be, "do nothing!" :)
On Ubuntu, python.el is provided by the emacs23-common package which is part of the standard emacs installation: