August 12, 2008 at 3:44 PM by Dr. Drang
My project notes usually include measurements, and I like to see them with typographically correct fractions and units. The correct characters are available through the Character Palette, but I don’t like keeping it open all the time, because
- it floats over all the other windows and obscures them; and
- it forces me to reach over to the mouse while I’m in the middle of typing.
So I created a set of TextMate snippets and macros to insert the fraction and unit characters from the keyboard.
The bundle is called Measurement, and you can get a zipped version of it here. After unzipping it on your hard disk, double click the
.tmbundle file to open it in TextMate. TextMate will import it and make it available to you.
Here’s what Measurement’s contents looks like in the TM Bundle Editor:
As you can see, most of the entries are simply snippets for inserting the single-character fractions, ⅛, ¼, ⅜, ½, ⅝, ¾, and ⅞. There are two snippets for each fraction: one for when the fraction is by itself and one for when it’s part of a mixed number. The Tab Triggers for the standalone fractions are their plain ASCII renditions:
1/4 for ¼,
1/2 for ½, etc. The Tab Triggers for the fractional parts of mixed numbers are the plain ASCII renditions preceded by a hyphen: to get 2¾, type
2-3/4, then press the Tab key. That’s a fairly common way to type mixed numbers without using the special fraction characters, so it seemed natural to make the Tab Triggers use that form.
The mixed number versions are necessary because TextMate will expand a snippet only if the Tab Trigger is preceded by a “word boundary.” TextMate considers a word boundary to be any place between an alphanumeric character and a non-alphanumeric character. For example, if you defined a snippet with the expanded text “education” and the Tab Trigger “ed,” pressing the Tab key after typing
- “ ed” (note the space) would expand the snippet to “ education”
- “-ed” would expand the snippet to “-education”
- “Fred” would not expand the snippet
- “2ed” would not expand the snippet
So, even with a Tab Trigger for ¾ defined as
3/4, pressing the Tab key after
23/4 would not generate 2¾, because there’s no word boundary between the 2 and the 3. But with a Tab Trigger for ¾ defined as
-3/4, pressing Tab after
2-3/4 does generate 2¾.
You could argue that the unhyphened versions of the Tab Triggers are unnecessary, and you’d be correct. But I like having the unhyphened versions because it seems more natural to type
7/8 instead of
-7/8 to get the standalone fraction ⅞.
There are two macros in the bundle for units: one for inch marks (″) and one for foot marks (′). These are not the straight quote characters, nor are they closing curly quotes. They are the single- and double-prime characters. Using straight quotes for inch and foot marks is a sign that you don’t care; using curly quotes is a sign that you have no visual taste whatsoever and should be shunned by civilized people everywhere. And you probably use Microsoft Word.
I started by defining the inch and foot marks as snippets, but that didn’t work because:
- I wanted to use
ftas the Tab Triggers;
- the word boundary rule for Tab Triggers means that typing “5in” and pressing Tab will not work;
- the marks must immediately follow the number—you can’t have “5 ″”; and
- I didn’t want to do a lot of fiddly editing, like this:
- type “5 in”
- press Tab to get “5 ″”
- arrowing back over the double-prime
- delete the space
- arrow forward past the double-prime
The solution is to use macros. Then I can type “5 in” and press Tab to launch a macro that deletes the preceding space before inserting the double-prime. The foot mark macro works the same way.
The remaining snippets are only of value if you happen to type things like “the bottom chord members of the roof truss are 2×6s” or “the slab is 15′ × 12′ in plan.” Note that these expand with the actual multiplication sign, not the “x” character, even though the “x” is used in the Tab Trigger for ease of typing.
(If you’re wondering why I don’t have a snippet for the degree symbol [°], it’s because that’s easy to type already: Option-Command-8. If the prime, fraction, and multiplication symbols were as easy to get to as the degree, I wouldn’t have made this bundle in the first place.)
I’ve set the scope for each item in the bundle to “text,” so they should work whether you’re writing in Markdown, HTML, or LaTeX. They should not work when you’re writing code—expanding
1/2 into ½ while programming seemed like a bad idea to me.