Update 04/08/2014: It seems like this post has been helpful for a lot of people, which really makes me happy! I just wanted to let new readers coming by know that the configuration I present in this post is out-of-date. However, I have my Emacs configuration on GitHub, and do try to keep it up-to-date. If you have any difficulties getting the configuration in the GitHub to work, please do submit a bug request and I will try to help you fix the problem.

As I mentioned in last week’s post, I am a heavy Emacs user and find it invaluable as an environment for my research. In particular, I use Emacs most for data analysis and modeling in Python, including an interface to IPython, which allows me to quickly switch between writing and running snippets of code. This proves to be a great replacement MATLAB, at least in my opinion.

I frequently get requests for my .emacs configuration file or questions about how I have such an environment set up. As such, in this post I’m going to document how to set up your Emacs configuration to support rapid Python development, including the plugins I have installed and the keyboard shortcuts I use most often.

Note that I am running GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK Version 2.24.5). I have never tried running it under OSX or Windows, so I can’t guarantee that these instructions will transfer, but you are welcome to try. If you run into any inconsistencies, feel free to send them to me and I will update this post accordingly!

Also, if you are new to Emacs, please read my beginner’s guide first, as I will be assuming familiarity with basic Emacs functionality and terminology. I will also assume you can at least read and understand LISP to some extent (but you don’t necessarily have to be able to write it).

Overall Goal

In this tutorial, we’re going to be installing several Emacs plugins and modifying the Emacs initialization file, which is typically found at ~/.emacs . Here are my .emacs and configuration files that it includes (note these are NOT the same as the actual plugins: I just like to divide my configuration so into bite-sized chunks so it’s easier to manage):

.emacs

ido-settings.el

auto-complete-settings.el

fill-column-indicator-settings.el

python-settings.el

ui-settings.el

window-settings.el

cursor-settings.el

text-settings.el

color-theme-settings.el

You can find a zip of all these .el files (excluding the .emacs) here. EDIT: The most recent version of my emacs configuration (which might not necessarily be consistent with this post) can be found here.

If you choose to install these configuration files, place the .emacs in the root of your home directory ( ~/.emacs ) and put the other files in ~/.emacs.d/settings .

A note about installing plugins: I’ve found that installing plugins from the Ubuntu repositories to be a bad idea because different versions don’t always play nicely with one another. It’s better to install the most recent version from source, and that’s what I’ve done for this tutorial. If you download and install the plugin versions (as I’ve indicated via the directories that I install them into), this should all work. Note that you may have to actually compile some of the plugins as well (if there’s a Makefile, run make; sudo make install and if there’s a setup.py file, run sudo python setup.py install . If there’s both, run the Makefile first and then run the Python setup).

Here is the list of plugins we’ll be going through/installing:

Non-Python Environment

There are several plugins that I use which aren’t related to Python coding, but are nonetheless very valuable: ido-mode (i.e., “Interactively Do Things”), general auto-completion, and the fill column indicator.

Interactively Do Things

“Interactively Do Things” (a.k.a. ido) is a plugin that should be included in Emacs by default; however, it is not typically enabled by default. You can enable it by running M-x ido-mode in Emacs, or by adding the following lines to your .emacs customization file:

```scheme Interactively Do Things (require ‘ido) (ido-mode t)

Ido is handy because it makes switching between buffers, opening/closing files, etc., extremely easy to do. Here's a few screenshots of what it looks like to switch buffers; notice that it lists in the minibuffer all the different buffers you could switch to: [![][24]][24] [24]: /assets/img/emacs-python/ido-switch-buffers.png The \*scratch\* buffer is bolded, which means it's the default. Let's say I want to switch to \*Messages\*, though. As I start typing the name, the list of buffers in the minibuffer is updated to show only those that match what you're typing (filtering) by: [![][25]][25] [25]: /assets/img/emacs-python/ido-switch-buffers-messages.png I could, at this point, simply press enter and it would switch to the \*Messages\* buffer. You get similar functionality when searching for files, killing buffers, etc. Saves a lot of time, not having to remember buffer names exactly and not having to fully type them out! There's a lot more to ido that I'm not going to go into; I encourage you to read through the documentation and find out more about it. ### Auto Complete [Auto complete][15] is another handy plugin which keeps track of recent things you've typed and will offer to auto complete them for you. Let's say I'm editing a python file: [![][26]][26] [26]: /assets/img/emacs-python/auto-complete-1.png And now I want to print out one of the arrays I just created. As I start to type, a list pops up with suggestions: [![][27]][27] [27]: /assets/img/emacs-python/auto-complete-2.png I can use the arrow keys to highlight the selection I want and press enter to choose it. [![][28]][28] [28]: /assets/img/emacs-python/auto-complete-3.png The variable name is completed and I don't have to type the rest, similar to the auto-completion behavior you'd see in Eclipse or another popular IDE. This is useful even if you don't have competing variable names -- if you have particularly long names, auto-complete will remember them, too, and you will only need to type the first few characters. To enable auto complete, [download it][29], install the files into `~/.emacs.d/auto-complete-1.3.1/`, and add the following lines to your .emacs: [29]: http://cx4a.org/software/auto-complete/#Latest_Stable ```scheme Auto Complete (add-to-list 'load-path "~/.emacs.d/auto-complete-1.3.1") (require 'auto-complete) (add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict") (require 'auto-complete-config) (ac-config-default) (global-auto-complete-mode t)

Fill-Column Indicator

It is good coding etiquette to keep lines short (e.g., no longer than 72 characters – this is because some people use 80 character terminals, and constantly scrolling to the left/right is very annoying!). It’s furthermore just nice coding style to have lines a consistent length.

To let myself know when I’m running over line length, I have the fill column indicator plugin installed. This draws a line on the right side of the screen, indicating the “fill column” (which is just another way of saying “the ideal maximum line length”). If my text runs past this line, I know I need to do some formatting to make it span multiple lines (if I’m just writing text, I will use M-q to wrap it).

To enable this plugin, download it and install the file in the ~/.emacs.d/fill-column-indicator-1.83/ directory, then add the following to your .emacs:

```scheme Fill Column Indicator (add-to-list ‘load-path “~/.emacs.d/fill-column-indicator-1.83”) (require ‘fill-column-indicator) (define-globalized-minor-mode global-fci-mode fci-mode (lambda () (fci-mode 1))) (global-fci-mode t)

## Python Environment Ok, moving on to the actual Python configuration now! First off, you'll (of course) need to have Python installed. I'm currently running 2.7. You'll also want to install [IPython][17] and I recommend you avoid the version in the Ubuntu repositories because it's out of date. You can download a newer version [here][32]; I am currently running a development version of 0.14. [32]: http://ipython.org/download.html Second, you'll need to actually [download][18] the `python-mode` plugin and install the files into `~/.emacs.d/python-mode-6.0.11/`. Add the following to your .emacs: ```scheme Python ; python-mode (setq py-install-directory "~/.emacs.d/python-mode-6.0.11") (add-to-list 'load-path py-install-directory) (require 'python-mode) ; use IPython (setq-default py-shell-name "ipython") (setq-default py-which-bufname "IPython") ; use the wx backend, for both mayavi and matplotlib (setq py-python-command-args '("--gui=wx" "--pylab=wx" "-colors" "Linux")) (setq py-force-py-shell-name-p t) ; switch to the interpreter after executing code (setq py-shell-switch-buffers-on-execute-p t) (setq py-switch-buffers-on-execute-p t) ; don't split windows (setq py-split-windows-on-execute-p nil) ; try to automagically figure out indentation (setq py-smart-indentation t)

Now open up a Python file and hit C-c ! . Another window will be created, and in it should be an IPython interpreter:

(I’m not entirely sure what’s up with the multiple In prompts at the beginning, but it doesn’t seem to matter so I haven’t bothered to investigate it as of yet.)

Not only can you run IPython inside of Emacs, but you can actually send code from the file you’re editing to the interpreter. To evaluate a snippet of code, first select a region:

Then hit C-c | to send it to IPython:

Now that the code’s been executed, you can refer to it in the IPython environment:

You can also send an entire buffer at once with C-c C-c :

python-mode also offers integration with IPython for tab completion. So, if you can’t quite remember the name of a function, just type what you can think of into the interpreter and press TAB. For example, if I type np.array and then TAB, a new buffer is created and shows the possible completions:

I actually dislike this behavior, however: my coding style is to have two windows open at once and to rapidly switch between them with C-c o . To have Emacs open a second window (or use an existing second window) and to maintain focus in the IPython interpreter, I’ve commented out the following lines in the ipython-complete function of ~/.emacs.d/python-mode-6.0.11/python-mode.el :

( with-output-to-temp-buffer "*IPython Completions*" ( display-completion-list ( all-completions pattern completions ))) ;; (set-buffer "*IPython Completions*") ;; (switch-to-buffer "*IPython Completions*") ;; (goto-char (point-min)) ;; (when ;; (search-forward (car (all-completions pattern completions))) ;; (forward-word -1) ;; (delete-other-windows) ;; (word-at-point)) )

This leads to behavior that instead looks like this:

Pymacs and ropemacs

Another handy set of plugins are Pymacs and ropemacs. To be honest, I don’t use them as much as I should – I’m hoping this post will re-familiarize me with the basic commands and I’ll start using them more!

Anyway, you probably don’t really need to worry about what Pymacs is: it’s an interface between Python and Emacs LISP, but we won’t be directly using that. What it’s necessary for is rope, which is a non-Emacs-specific Python refactoring library. Ropemacs is just the Emacs plugin for rope.

To install, first install rope itself:

sudo aptitude install python-rope

Now download the Pymacs code and install it into ~/.emacs.d/pymacs-0.25/ . Download the ropemacs code and install it into ~/.emacs.d/ropemacs-0.7/ . Add the following to your .emacs:

```scheme Pymacs and Ropemacs ; pymacs (add-to-list ‘load-path “~/.emacs.d/pymacs-0.25”) (autoload ‘pymacs-apply “pymacs”) (autoload ‘pymacs-call “pymacs”) (autoload ‘pymacs-eval “pymacs” nil t) (autoload ‘pymacs-exec “pymacs” nil t) (autoload ‘pymacs-load “pymacs” nil t) (autoload ‘pymacs-autoload “pymacs”)

; ropemacs (require ‘pymacs) (pymacs-load “ropemacs” “rope-“)