TextMate: Regular Expressions

June 17th, 2006

NOTE: This article is outdated. I have since created a bundle which (in cutting-edge versions of TextMate) can be installed by unpacking the zip file and double-clicking on the RegExp.tmbundle file. Help is available within the bundle.

TextMate currently displays regular expressions just like a string, no help for the developer at all. So I decided to change that.


Background Color

This is a really easy modification that makes it easy to see if a regular expression has some unbalanced parenthesis of delimiters: Give them a background color! Now when they are unbalanced it will jump out at you! In the Fonts & Colors preferences add a new element, name it Regular Expression, give it a scope of string.regexp, click on the BG column and assign a color with a low Opacity. And there you go: your regular expressions will now have a subtle highlight.

Regular Expression Language

The real power of TextMate can be leveraged when we add a Regular Expression Language. With it’s help (and some additional required changes described in the next sections) the formerly mono-colored regular expressions come to life. Various components in the regular expressions will be colored differently, and ⇧⌃P will now give you a much better idea what the part of a regular expression under the cursor is doing.

Activating Regular Expression Language use in Perl

Just installing the Regular Expression Language alone will not do the trick, we also need to tell TextMate where to use it. Here is my personal Perl Language, which amongst other things makes use of the Regular Expression Language. After installing both the Regular Expression Language and my Perl Language, change the language for your perl file to Perl (Gerd) and the regular expression syntax will become active.

Activating Regular Expression Language use in TextMate Language Definitions

Here is my personal Language Definition for TextMate. However installing it does not yet render the regular expressions: Due to who-knows-what, the default Language Definition includes the old-style Property List syntax, which promptly takes over and prevents coloring of language definitions. To fix that, go to the Bundle Editor, select the Language Definition language in the TextMate bundle, and scroll to the bottom. Delete this line:

{ include = 'source.plist'; },

and click the Test button. You can check the effects easily: Copy the entire Language into a TextMate window, and set the language to Language Definition (Gerd). You should now have a nicely colored version of the language, and the regular expressions should be using the new regular expression language.

Implementation Notes

You will notice that the Regular Expression Language is actually named Perl Regular Expression. While regular expression engines use very similar syntaxes, they all have their own quirks. My Regular Expression source code is actually a modified plist with comments and conditionals thrown in. That will in future allow me to generate custom Regular Expression Syntaxes for the various languages. And hopefully some day TextMate will support conditionals within patterns.

Of course all of this is highly experimental. I am not sure if Allan will approve of the scope names used; I tried to make reasonable use of them while at the same time trying to achieve a sensible syntax highlighting of the regular expressions. And there are a bunch of special conditions which the Regular Expression Language does not take into account, either beacuse I just didn’t add or properly handle them yet, or because they go beyond the limits of TextMates current syntax highlighting engine.

Entry Filed under: OS X, TextMate


April 2018
« Jun    

Most Recent Posts