A few months ago, I switched from GNU Emacs, which I’ve used for more than 20 years, to Sublime Text 2. After years of using Emacs, there are a few things I miss in Sublime Text 2. Fortunately, Sublime Text 2 has a rich Python API, and it supports plugins; so, it’s relatively easy to add features I miss.
Like all decent modern programming editors, Sublime Text 2 supports syntax
highlighting. But, for various reasons, it can’t always guess which syntax
applies to a file. For example, lately, I find myself editing a lot of Sass
files. Sublime Text 2 always brings Sass files (i.e., files ending in
up as plain text files, with no syntax highlighting. I wanted a way to tell
Sublime that all files ending in
.scss should be assigned the “Ruby Sass”
syntax, by default.
In other words, I wanted the equivalent of this Emacs Lisp capability:
There are various existing plugins that almost do what I want, kind of. For example, Phillip Koebbe’s DetectSyntax plugin is, basically, an extensible rules engine that can determine the syntax based on a file name, the contents of the file, and many other conditions. It’s even customizable via callbacks. But, while DetectSyntax would surely do what I need, I wanted something a little simpler. (However, by all means, take a look at DetectSyntax. It’s good work.)
So I wrote one. Called Sublime Text 2 Syntax from File Name (ST2SyntaxFromFileName, for short), it simply maps a file name regular expression into a syntax name.
The plugin is configured via the
filename_syntax_settings value in the user
settings (accessible via the “Preferences → Settings - User” menu). That
value is an array of arrays, and each inner array element defines a mapping
from a regular expression to a syntax name. For instance, here’s a portion of
my settings file:
1 2 3 4 5 6 7 8 9
Those settings map files ending in
.sass to the “Ruby Sass”
Each entry has two or three fields. The first two are mandatory. They are:
- A regular expression pattern against which to match the filename. Note that backslashes must be double-escaped, because of the way the JSON parser works.
- The syntax value to apply to matching files. The name must match the name of a
.tmLanguagefile somewhere underneath the Sublime Text 2
Packagesirectory. The name is matched in a case-blind manner; thus, “Ruby” and “ruby” mean the same thing.
- Optional flags for the regular expression parser. Currently, only “i”, for case-blind comparison, is honored. Anything else is ignored.
The settings examined applied in order, and the first match wins.
While this plugin is far less powerful than the more general-purpose DetectSyntax plug Phillip Koebbe wrote, its simplicity suits my purposes exactly. It’s also a nice demonstration of how easily one can extend Sublime Text 2.