TextMate: Regular Expressions

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.

June 17th, 2006

TextMate: Smarter Regular Expression Syntax for Perl

Regular expressions are hard to read. TextMate could probably help by having a syntax for regular expressions, but we’ll keep that for another day. A simple first step though would be to make this:


easier to read by giving the find_me and the replace_with_this parts their own scope. That is easily done: In TextMate, open the bundle editor, and find the perl syntax definition. Near the beginning of the patterns section you will find this definition for string.regexp.perl.replace:

{   name = 'string.regexp.perl.replace';
    match = '\b(s|tr|y)\s*([^A-Za-z0-9\s])(.*?)(?<!\\)(\\{2})*(\2)(.*?)(?<!\\)(\\{2})*(\2)';
    captures = { 1 = { name = 'keyword.function.perl'; }; };

Replace the captures line with this fragment:

captures = 
{   1 = { name = 'keyword.function.perl'; };
    3 = { name = 'string.regexp.perl.replace.search-part'; };
    4 = { name = 'string.regexp.perl.replace.search-part'; };
    6 = { name = 'string.regexp.perl.replace.replace-part'; };
    7 = { name = 'string.regexp.perl.replace.replace-part'; };

Now the string.regexp.perl.replace.search-part and string.regexp.perl.replace.replace-part scopes are available for syntax highlighting.

June 14th, 2006

Using ctags in TextMate

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 CTags.tmbundle file. Help is available within the bundle.

To make navigating through code easier in TextMate I wrote this perl script a while ago. It makes use of the Exuberant ctags to create a tags file of an entire project, which can then be used to search for the definition of functions etc.


The script requires the Exuberant ctags program. Easiest is to just install TmCodeBrowser, the script knows how to find the ctags program contained in it. Alternatively install ctags elsewhere and set the $CtagsPGM variable at the beginning of the script to contain the entire path.


  • Install ctags as described above
  • Download this perl script and put it somewhere
  • In TextMate create a command named Update Tags with these settings:
    • Save: Nothing
    • Command(s) ‘/path/to/tmctags’ update
    • Input: None
    • Output: Show as ToolTip
    • Activation: pick a Key Equivalent of your choice
  • In TextMate create a command named Find Tags with these settings:
    • Save: Nothing
    • Command(s) ‘/path/to/tmctags’
    • Input: None
    • Output: Show as HTML
    • Activation: pick a Key Equivalent of your choice


In any project with source code the Exuberant ctags program can handle, use the Update Tags command created above. This will create a file named tmtags at the projects root. If you are not in a project, the script will search for a tmtags in the directory where the current file is and in all parent directories. If so it will use that location, otherwise it will create the tmtags file in the current files directory. A ToolTip will let you know where the file was created/updated.

Once the tmtags file is created, use the Find Tags command created above to find the definition(s) of any function currently under the caret. The script will create output similar to this:

Picture 1.jpg

Click on the blue link to jump to that file and line.

May 10th, 2006

OS Y: Experimental Web Application Environment

Windows and Dock in JavaScript

Like so many things, OS Y started with I wonder if….

I was thinking about updating the look of my site for OS X specific products, www.cocoabits.com. But I did not want to create yet another web site look where folks have to figure out how it works first. It would be neat if it has some connection to OS X, as OS X users are the target group of this site. So there it was: I wonder if I could create something Dock-like on a web page…. To my surprise that wasn’t all that hard, though a lot trial and error to get the effects look halfway decent.

From there the obvious next step was that clicking the icons in my dock would open windows. So a simple JavaScript-based window manager was next.

First all code was targeted to Safari users only. I know the nightmares web developers go through trying to get CSS and JavaScript to work similar on the large number of browsers and their versions, and did not want to go there. But I checked some. To get it to work on FireFox did only require a few tweaks, FireBug made that task pretty easy. Once it worked on FireFox, it also worked OK on Mozilla and Camino, with the exception that moving windows behaves erratic. Kind of odd, as the code is similar to the resize code which appears to work fine… Next I checked was OmniWeb. Given that like Safari it is based on Apple’s webkit, I did not expect a problem. But to my surprise it doesn’t seem to do anything at all… Though OmniWeb does have a JavaScript Console, it stays mysteriously silent. And there seems to be no documented way to print debug messages to the JavaScript Console to aid debugging. IE Mac does not appear to do anything either, but with all it’s known bugs that was no surprise.

You can try OS Y on your browser here.

Whereto from here

I believe the idea of an ‘OS’ in the browser has potential. In the (real) olden days there used to be main frames and terminals. Then PC’s became so powerful that mainframes became more and more obsolete. But I believe the future will see web browsers as terminals to the Internet MainFrame. People will be able to access their data from everywhere there is a web browser. IMAP made this possible for EMail, web based EMail browsers such as Squirrelmail allow to view and organize EMail from any web browser. From there it is not really a big step to web based word processors, spreadsheets, Photo albums PIM etc., with personal data stored on some data farm, accesible from everywhere. Many of these solutions already exist. Maybe the computer tomorrow is nothing but a web browser, and we will see a renaissance of WebTV-like appliances.

As far as OS Y is concerned, who knows. A menu bar would be the next logical addition, then some real applications and not just web sites making use of OS Y’s functionality. And maybe it’ll get me a job at Google…

March 5th, 2006

WordPress plugin gkAlphaPosts

My wife uses WordPress to keep tabs on her knitting books. She doesn’t have much use for the time-based archive listing, and askd for an alphabetized listing instead. The result is gkAlphaPosts, my first WordPress plugin. Maybe someone else finds use for it.

Homepage: http://gerd.knops.org/gkAlphaPosts/
Download: http://gerd.knops.org/gkAlphaPosts/gkAlphaPosts.current.zip

January 29th, 2006