Select Balanced HTML Tag

hey kids

just wanted to share with you my latest textmate innovation.

There is a bit of standard functionality that major html editors have been capable of forever

In BBEdit it’s called View > Balance

In Dreamweaver it’s called Edit > Select Parent Tag

What this does is select the current html tag, no matter where your caret is in between the opening and closing tags.

Ok, that’s all fine and lovely, but I use TextMate. And while TextMate has an Edit > Select > Enclosing Brackets function. That doesn’t actually DO anything in HTML. Nothing useful anyway.

So, what is the solution? How do we actually make this work in TextMate?

Well, that’s where I come in. TextMate is built on a community of developers who spend time extending and expanding on what TextMate is capable of.

So, I have been goofing around with ruby scripts and insanely complex regular expressions since 2005 trying to make this work.

And I have finally managed to come up with something that is simple and uses the normal standard TextMate Select > Enclosing Brackets functionality.

I call it Select Balanced HTML Tag and I have it bound to ⌘⇧B

Let’s bring up TextMate

Put the caret inside of an HTML tag that we want to select, and hit the magic shortcut key: ⌘⇧B

And she-bang! We have our tag selected.

Then you can keep hitting the keystroke to expand your selection to each succesive enclosing tag.

BUT wait, there’s more!

My “Select Balanced HTML Tag” macro is UNDOABLE!

HAHA! Take THAT HTML! Bet you didn’t see THAT one coming!

Yes, sure. Dreamweaver has the “Edit > Select Child” command, but I find that it doesnt actually DO anything most of the time.

I must admit however that the Dreamweaver and BBEdit versions of this command actually work a LOT better and WAY WAY faster, but I find that reasoning completely useless because I’m never goign to be using either of those applications.

So to sum up.
New TextMate Macro Select Balanced HTML Tag
Selects the current HTML Tag,
similar to the BBEdit Balance and Dreamweaver Select Parent Tag commands.
Except that it’s UNDOABLE and works in TextMate and is much crappier.

Written by me, Thomas Aylott.

You can download this at