Without a question my favorite Mac app currently is TextMate. TextMate has a lot of power in itself, but it wouldn’t be near as powerful as it is without it’s Open Source like community (even though the app itself remains Closed, developed by Allan Odgaard). Since there is no way that I could ever sum up the power of TextMate in one post I’m going to have TextMate series covering different parts of TextMate in different posts. This is part one on Snippets.
Snippets are little bits of text that expand to be huge amounts of text and provide the simplest form of automation in TextMate with either a Tab trigger or Key command. Not only can snippets expand but can have Placeholders that you just Tab to add in some text and Tab again to the next place. It’s a little hard to explain so here’s a picture:
Before using Tab trigger:
After using Tab trigger:
So as you can see it expands the text, and has some text selected already for me that I can change or hit Tab again and go on to some more text.
For making your own here’s what this snippet looks like in what’s called the “Bundle Editor” (where you make snippets for what are called Bundles which hold all your Snippets, Commands and so forth):
< %= link_to "${1:link text...}", :action => “${2:index}” %>
The “< %= %>” is part of Rail’s syntax, there nothing really going on here. The ${1:link text…} however has quite a bit going on. The “${ }” signifies that the text inside of the brackets is going to be selected. The number sets the points at where the cursor will end up after each Tab (in this case after hitting 1 Tab will have “link text…” selected). For creating snippets where you don’t want selected text you would just use “$INSERT_NUMBER”. For example:
This would be the position after 3 Tabs:
This is produced by this snippet:
< *div ${1: id="${2:name}"}>
${3:$TM_SELECTED_TEXT}
< */div>
I added in *s because the pre tags don’t work for some reason.
Because I didn’t have any text selected it behave like a regular simple Tab trigger ($3) however if I had of had anything selected it would be inside the div.
There’s a bit to get you going on Snippets and TextMate which you can check out here.
Note: A lot of stuff that I’m going to cover is on the Dev’s site, however hopefully I’m going to make it easier to understand for new-to-intermediate users.
Note 2: For further reading on TextMate’s Snippets, go here.
Note 3: For Vim users there is SnippetsEmu which attempts to bring the same functionality to Vim.
Brad Jasper
Specifically I'd be interested in bundles for PHP, JavaScript and XHTML.
Nice tip Travis, especially the vim part--I see myself using that frequently at work. December 27th, 2007 at 9:47 pm
Travis Jeffery