TextMate Pt.1: Snippets

Icon Textmate

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:
snippets

After using Tab trigger:
after

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:
div_trigger

This would be the position after 3 Tabs:
_after_tab

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.

Similar Posts:

I Like This Tip!


2 Responses to “TextMate Pt.1: Snippets”

  1. I was curious if there were default Bundles to use or you had to create them all yourself.

    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.

  2. Yeah there are a lot of bundles already made. Here's a link on how to download the Bundle Repository. After you've followed the steps on the site though you're going to have a lot of Bundles that you don't use so go to the Bundle Editor --> Filter List and choose the Bundles that you want to use. However I really recommend tweaking them with your own stuff that you type a lot and can be more efficient at.

Post a comment to "TextMate Pt.1: Snippets"



MacTips Newsletter


Twitter MacTips

  • Stay up to date with the latest Mac tips, tricks and news by following us on twitter: MacintoshTips.

AppleGazette Headlines

Most Popular Tips (30 Days)

Most Commented (30 Days)

Good Tips

Recent Discussions

MacTips Poll

What level of tips would you like to see?

View Results

Loading ... Loading ...

Friends of MacTips

Subscribe to MacTips

Site Links