Creating a new Python command in TextMate

August 27, 2006

I’ve been playing with TextMate for all of about one day, and already I find I’m proficient in it to start writing my own extensions to the Python bundle that comes with the editor.

There’s a neat command in the Ruby bundle that evaluates a line or a selection and inserts the result as text. I wanted something similar for Python. Here’s what I did:

First, I went to the Python section of the Bundle editor and created a new command, which I titled Execute Line/Selection as Python. In the editor for that command I typed the following:

#!/usr/bin/env python
import os
from code import InteractiveConsole

console = InteractiveConsole()

region = os.environ.get("TM_SELECTED_TEXT")
if not region:
    print "n"
    region = os.environ.get("TM_CURRENT_LINE")

console.runcode(region)

That’s all. Everything seems to work thus far. What I find amazing about this is that I’ve been using Emacs for 22 years now, and I’ve never done anything anywhere near this sophisticated, despite having learned elisp for the expressed purpose of hacking Emacs. And yet in less that 22 hours I’ve already expanded the environment.

I’m also writing this in TextMate, of course. I really love Emacs. The first thing I did in my undergraduate research group was learn Emacs. The first thing I did in my graduate research group was to install a good version of Emacs. The first thing I did in my postdoctoral research group was to install a good version of Emacs. By the time I got to my own research position, Emacs was bundled almost everywhere, and I use it almost nonstop. TextMate is the first editor in years that has made me think twice about Emacs. And after only one day. Wild.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: