Windows lacks a decent programmable text editor.

I know there’s Emacs and Vim, but I want something that looks good, and is easy to learn. If it’s free or cheap, that’s good, too. Based on screencasts I’ve seen, I’d say I want something like TextMate, but I’ve never actually used it, so that’s just a guess.

It has to be programmable. I spend lots of time working with text, and I want an editor that lets me write code to process my text. I want to take a list of values, and generate SQL from it. I want to auto-format HTML. I want to insert custom code templates, based on keywords I type.

Really, I want something like Ruby’s text-mashing power, built into my editor. If I could send a buffer’s contents through some code, and replace the buffer with the script’s output, that’d be something. This would let me automate probably 99% of the repeatable things I do. It doesn’t have to be Ruby, but that’d be nice.

EmEditor’s Programmable Macros

I found EmEditor, which gets me pretty close. It has a 30-day trial, and it’s $40 to buy. It has programmable macros, with a decent set of variables that give you access to the buffers, editors, and all menu items. The macros are run through the Windows Script Host (WSH), which is a little scary, but it means that you can write your macros in JavaScript. Actually, since WSH is language-agnostic, you can write your macros in any language with a WSH interface. Including Ruby. Or Haskell, or Lua, which makes it easier for me to experiment with them now. And of course there’s Python and Perl. I think this is pretty nifty.

The Downside

Since it’s built on WSH, you don’t have any means of including other files. Nothing like Ruby’s require, Java’s import, C#’s using. This means no library re-use, no utility functions. No RedCloth for blog posting, no REXML for auto-formatting XML, no Hpricot for digging through generated HTML. How about a nice, clean interface to WSH’s hairy objects? I’d love to write one! (Well, I’d love to have one, so I’d be willing to write it.) But you couldn’t use it, if I did. I think this is a pretty significant problem. You have to start from scratch with each script. Someone please tell me I’m missing something here.

None of this is stopping me from rolling up my sleeves yet. Here are some tasks I’ve automated so far (you can get some of the .js files from my Google Pages…I’ll put up more when I have a minute):

  • Replacing “smart quotes” and other funny characters with their plain-text equivalents. Great for copying code and data out of MS Office products.
  • Delete all copies of whatever text is selected. When I’m cleaning up generated HTML, I remove lots of code (<td>, <tr>, class="foo"), so I can see the parts I care about. Just highlight, run, poof. All gone.
  • Make the word I just typed into an opened-and-closed HTML tag, with the cursor in between. I have this hooked up to Ctrl-space, and Shift-Ctrl-space adds in a newline and tab indent. “b” ctrl-space produces <b>|</b>, and the cursor is where the | is. “table” shift-ctrl-space “tr” shift-ctrl-space “td” ctrl-space. Much faster.
  • Run some JavaScript on each line, and replace the line with the code’s return value. You can use this for chewing on data, cleaning up formatting, calculating values, or whatever. If you have to change many lines of similar C# or Java, you can chew each line up, and rewrite the code with this. Powerful, easy to make a mess with, and all that. This is probably my favorite.

Things I haven’t gotten around to yet:

  • Tidying XML or HTML
  • Customizeable templates. These would work like my HTML-tag-izer, where you type a word, then Ctrl-space, and your word is replaced with a template of code. Eclipse has this, and TextMate comes with a whole mess of really nicely defined ones for Ruby.

Maybe someday, I’ll have learned emacs, and I’ll look back on this post and laugh at myself. “Windows Script Host?? What was wrong with me?” In the meantime, I have something that helps me along, lets me have fun automating things, and lets me explore other languages.