Python Mode for Emacs

Type C-c? when in python-mode for extensive help. Also see the Emacs OO-Browser support for Python.

Indentation

Primarily for entering new code:

TAB

Indents line appropriately.

LFD

Inserts newline, then indent.

DEL

Reduces indentation, or delete single character. Primarily for reindenting existing code.

C-c:

Guesses py-indent-offset from content; changes locally.

C-u C-c:

Same, but changes globally.

C-c TAB

Reindent region to match its context.

C-c <

Shifts region left by py-indent-offset.

C-c >

Shifts region right by py-indent-offset.

Processing Regions of Code

C-c C-b

Marks block of lines.

M-C-h

Marks smallest enclosing def.

C-u M-C-h

Marks smallest enclosing class.

C-c #

Comments out region of code.

C-u C-c #

Uncomment region of code.

Moving the Point

C-c C-p

Moves to statement preceding point.

C-c C-n

Moves to statement following point.

C-c C-u

Moves up to start of current block.

M-C-a

Moves to start of def.

C-u M-C-a

Moves to start of class.

M-C-e

Moves to end of def.

C-u M-C-e

Moves to end of class.

Executing Python Code

C-c C-c

Sends the entire buffer to the Python interpreter.

C-c |

Sends the current region.

C-c !

Starts a Python interpreter window; this will be used by subsequent C-c C-c or C-c | commands.

Variables

py-indent-offset

Indentation increment.

py-block-comment-prefix

Comment string used by py-comment-region.

py-python-command

Shell command to invoke Python interpreter.

py-scroll-process-buffer

‘t’ means always scroll Python process buffer.

py-temp-directory

Directory used for temp files (if needed). ...

Get Python Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.