Hacking on the Sublime Text 2 Syntax Highlighting

Sublime Text 2

This morning I was getting tired of the pretty lame syntax highlighting of YAML files in Sublime Text 2 - and I know it could be better. So I started digging. The first thing I looked at was the tmTheme file that I can cloned of the Eiffel theme in the standard release package. It's close… white background, nice colors, but it's not perfect, and I wanted perfect. So here's what I found out.

The matching of the language is really in the tmLanguage files in the packages. These are a bunch of regexs, and it's fine, but each pattern match then pins the color to use to some "classification" - a dotted-notation similar to a Java package. The idea is that if you specify only the first part or parts, then the last parts are up for specialization.

For instance, if you want to have a numeric constant style, it makes sense to build them hierarchically: constant -> numeric -> yaml, this leads to the classification: constant.numeric.yaml. But if you want all constants to be a certain style (by default), you can simply specify the constant style in your tmTheme file.

Alternatively, if you want all your numeric constants to be a certain style except those in java, you make a style for constant.numeric and then a new one for constant.numeric.java. Simple. But certainly not simple to figure out by looking at the files.

So I realized that for YAML, I didn't want the 'Embedded source' to have a colored background. So I added:

  1. <dict>
  2. <key>name</key>
  3. <string>Embedded source</string>
  4. <key>scope</key>
  5. <string>source.php.embedded.block.html, string.unquoted.yaml</string>
  6. <key>settings</key>
  7. <dict>
  8. <key>background</key>
  9. <string>#FFFFFF</string>
  10. </dict>
  11. </dict>

so now it's got a white background. Nice.

The next thing was to notice that I didn't like that the keys in YAML were red like almost all the text (strings, constants, etc.) so I wanted to make those keys blue:

  1. <dict>
  2. <key>name</key>
  3. <string>Markup name of tag</string>
  4. <key>scope</key>
  5. <string>entity.name.tag.yaml</string>
  6. <key>settings</key>
  7. <dict>
  8. <key>fontStyle</key>
  9. <string>bold</string>
  10. <key>foreground</key>
  11. <string>#1C02FF</string>
  12. </dict>
  13. </dict>

and now the keys are a nice blue. Much better!

All this is just in my clone of the Eiffel theme in the Packages/User/ directory in the Application Support for Sublime Text 2. Very nice.

UPDATE: I realized it should be easy to do the same for PHP - which has the annoying background color, and it was! You simply have to look into the tmLanguage file and see the tag name that's used and place it in the string in a simple comma-delimited list. Very slick!

UPDATE: I noticed a few more that I wanted to add - all from the HTML syntax highlighting. The code became:

  1. <dict>
  2. <key>name</key>
  3. <string>Embedded source</string>
  4. <key>scope</key>
  5. <string>
  6. source.php.embedded.block.html,
  7. source.css.embedded.html,
  8. source.js.embedded.html,
  9. source.python.embedded.html,
  10. source.ruby.embedded.html,
  11. string.unquoted.yaml
  12. </string>
  13. <key>settings</key>
  14. <dict>
  15. <key>background</key>
  16. <string>#FFFFFF</string>
  17. </dict>
  18. </dict>