[Skip To Content]


# Editing TextMate's Syntax Highlighting

I generally use TextMate for text editing these days, so I’ve slowly being building up a bunch of custom modifications to the stock install. The most obvious of these modifications is changing the colours of the syntax highlighting, which matches the code snippets you see on this blog.

If you use TextMate yourself, you’ll know that it supports highlighting multi-line comments differently from single-line comments, but if you’re a Ruby coder, you’ll know that the only kind of multi-line comments it supports is the nasty =begin/=end thing which can’t have any whitespace preceding them and thus messes up your nicely indented code :

  1. module Supermodel
  2. =begin
  3. This class encapsulates all information about a given index.
  4. Blah blah blah.
  5. Blah.
  6. =end
  7. class Index
  8. def initialize()
  9. # Do stuff
  10. do_stuff
  11. end
  12. end
  13. end

Ugly.

What I ideally want is some way of using the same multi-line highlighting (with the dark grey background) but without having to use =begin and =end, and without having to bind everything to the beginning of a line.

And that’s now what I have, this is a screen grab from TextMate (I’ve not got round to mirroring the changes on the blog yet) :

You’ll notice I’m using ## to mark a line which should be highlighted in that manner, separately from a normal comment beginning with #.

And here’s how I did it!

TextMate’s syntax highlighting turns out to be pretty fantastic and flexible. You can read about it here and here and here, and probably on some other pages on that site too.

The first step is to define a rule for the new highlighting type. From within TextMate, if you go to the Bundles → Bundle Editor → Show Languages menu option, you’ll get a list of languages that TextMate will highlight for. Open the Ruby node in the tree-view, and you’ll see one language – Unsurprisingly, called ‘Ruby’. I copied this, creating a new language called ‘Ruby (double-number-sign highlight)’.

Having read the above about scoping, I changed the scopeName at the top of the new language definition to source.ruby.double-number-sign-highlight to avoid any crazy clashes that might occur otherwise. This might be unnecessary, but it seems correct.

If you then search the language file for comment.line, you’ll get to the place where we need to add our new rule. Here’s the new rule we’re going to add :

  1. { name = 'comment.line.double-number-sign.ruby';
  2. match = '(?:^[ \t]+)?(##).*$\n?';
  3. captures = { 1 = { name = 'punctuation.definition.comment.ruby'; }; };
  4. },

You can see it’s very similar to the existing comment.line rule. Make sure you add that rule above the existing one, the rules seem to be traversed in order until one matches, and obviously a rule looking for a line beginning with # will match one beginning with ##.

Now that we have the new rule set up, we just need to tell TextMate how to use it. Close the Languages window, and then go to TextMate → Preferences → Fonts & Colors. You’ll see the usual colours you use for your highlighting. To add a new highlighter, click the + button, and a new rule will appear. Set its name to something like ## comment, and set the colours you want to use. Then, importantly, change the Scope Selector entry to read comment.line.double-number-sign (Note that although the field in a drop-down menu, you can type new entries into it too.)

And that’s it! If you open up some existing Ruby code, or create a new file, and ensure that you use the Ruby (double-number-sign highlighting) language (You can set that using one of the menus at the bottom of the TextMate window), any comments you create with ## will be highlighted differently to #.

If you don’t want to go through all the hassle of setting this up yourself, you can download a disk image containing the new language and a theme that highlights the double-hash-sign below :

To install them, just double-click on the files once you’ve mounted the .dmg. They’ll be copied by TextMate to ~/Library/Application Support/TextMate, from where you can delete them if you want.