Emacs: Indenting Python Code

By Xah Lee. Date: . Last updated: .

in the default python mode shipped with emacs 24.2, you can do this to shift the indentation.

You can also use kill-rectangle and related rectangle commands [see Emacs: Edit Column Text, Rectangle], but that's more manual work and less accurate.

Note: if you have cua-mode on, the keys won't work (because C-c does copy.). One solution is to press the complete key sequence within a fraction of a second. Another solution is to hold Shift down, but this method won't work in terminal. Better is to assign it another key. [see Emacs Keys: Define Key]

Here's a dummy code you can play with.

# dummy code for testing indentation
for x in my_list:
    if len(x) != 2:
        sys.exit("Error.")
        if len(x) != 2:
            sys.exit("Error.")

( thx to boostjam, [Matthew Fidler https://plus.google.com/100239989767196954209/posts])