I recently switched to TextMate for editing Stata .do files: unlike Stata’s built-in editor on the Mac, it has syntax highlighting and other goodies via Timothy Beatty’s bundle (now hosted by Dan Byler).

One thing it doesn’t have, though, is tab completion. Or rather, it didn’t, until now.

What does it do?

It’s the same thing you get in Stata’s Command window: you type part of a variable name, press tab, and the name is completed as far as it can be without ambiguity.

For example, I type —

— I press tab —

— and the variable name is completed, leaving the cursor one space to the right and ready to keep typing. (Note that my use of Stata’s tab[le] command in these examples is an unfortunate coincidence and has nothing to do with tab completion! I could just as well have written regress fr. Sorry.)

In fact, proper tab completion (as found in Unixy shells, for file names, but not in Stata’s Command window) goes a little further than this: if you press tab at the end of an ambiguous name stem, it prompts you with all the possible endings.

And so it is with this TextMate bundle addition. I type —

— I press tab —

— and the name is completed up to the next ambiguity. So I press tab again —

— and I get a tooltip list of options. I add an ‘s’ (just enough to pick one option out) —

— press tab again —

— and bingo!

How does it work?

Each time you press tab, the bundle asks Stata to run a simple .do file; this exports your current data set with an if 0 condition, meaning that all the actual data is excluded, and we just get the variable names. (For detail, check the Ruby script).

A slightly unfortunate effect of this is that your Stata Results window (and log, if you’re keeping one) fills up with these outsheet commands. I hope that’s not a deal-breaker. If only Stata would provide some basic AppleScript support (please?) I might be able to do something about this.

How do I get it?

At some point I may roll this into a new bundle file to make installation easier (there are also a few fixes I’ve made to the original bundle). But in the meantime, you can add it to the bundle yourself.