1. Technology

Emacs Tips for Python Programmers

By September 24, 2007

Emacs is one of the most powerful Python editors available today. It runs on every major platform (and many minor ones) and makes editing very easy. Aside from being scriptable in Python, Emacs' Python mode comes with a plethora of shortcuts to make Python programming easier on your hands and your head. It starts up automatically whenever you open a Python program in Emacs. Here is a brief summary of Emacs' Python mode (Update of 29 May 2008: Per Dave Turvene's comment, it is worth noting that these bindings are from the Python mode proferred by the PSF and available at http://sourceforge.net/projects/python-mode/):
  • C-j: Insert a new line with the same indentation level as the current line
  • RET: Insert a new line with the same indentation level as the current line
  • C-M-a: Go to the beginning of the current function or class
  • C-M-e: Go to the end of the current function or class
  • C-M-h: Mark the current function or class for copying, etc.
  • C-M-x: Execute the current function or class
  • C-c C-b: Submit a bug report
  • C-c C-c: Execute the buffer (i.e., the file being displayed)
  • C-c C-d: Trace the stack of the process being executed
  • C-c C-h: Get context-based help
  • C-c TAB: Indent a highlighted (or marked) region
  • C-c C-k: Mark a block of text. Using this at the head of a class or function definition will mark the entire block.
  • C-c C-l: Shift the region to the left. If the cursor is in the middle of a region, the lower half of the region will shift.
  • C-c RET: Execute the current file, opening a new window to show the output.
  • C-c C-n: Jump to the next statement.
  • C-c C-p: Jump to the previous statement.
  • C-c C-r: Shift the region to the right. If the cursor is in the middle of a region, the lower half of the region will shift.
  • C-c C-s: Execute a Python command.
  • C-c C-t: Toggle shells
  • C-c C-u: Go up one block
  • C-c C-v: List the version of the Python mode
  • C-c C-w: Run PyChecker
  • C-c !: Open the Python interactive shell
  • C-c #: Comment the highlighted (marked) region
  • C-c :: Check the indentation off-set
  • C-c <: Shift the region to the left
  • C-c >: Shift the region to the right
  • C-c ?: Show Python mode documentation
  • C-c |: Execute the highlighted (marked) part of the current program.
Note that Python mode for Emacs offers more functionality than this, but these are the basics. If you know Emacs from editing other languages, learning your way around the Python mode is a snap.
Comments
May 28, 2008 at 3:45 pm
(1) Dave Turvene says:

Nice write-up but I’m a little confused.

These keybindings look to be from the python.org python-mode package. The native emacs22 progmodes/python.el is different, though both are invoked as “python-mode”.

It would be useful to clarify which emacs and which python-mode is being used.

May 29, 2008 at 12:30 pm
(2) python says:

That is a good point, Dave. I have updated the post to make it clearer. Thanks for pointing it out.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.