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 Commands〕, 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: How to Define Keys

Here's a dummy code you can play with.

# dummy code for testing indentation
for x in my_list:
    if len(x) != 2:
        if len(x) != 2:

( thx to boostjam, Matthew Fidler)

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs