Posts filed under 'TextMate'

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 = ''; };
    4 = { name = ''; };
    6 = { name = 'string.regexp.perl.replace.replace-part'; };
    7 = { name = 'string.regexp.perl.replace.replace-part'; };

Now the 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


May 2017
« Jun    

Posts by Month

Posts by Category