Better: Better with Textmate Bundles & Snippets
Copy this link for a short URLThis entry rated T for TRON! TRON IS COMING BACK!
Many, and most of this information is gleamed from the wondrisplenderous book TextMate which you should all buy immediately if Textmate is your editor. If you’re illiterate, then we’ve found ourselves at a supposition, because you are a liar and a crook. There’s nothing you can do about it you ugly bastard because you can’t read this.
Begin Bundle
Building your own Textmate bundle is quite easy to do. Here are the steps for creating one.
- Open Textmate
- Type “CTRL + ALT + CMD + B”
- Click on the plus arrow in the bottom right corner and select “New Bundle”
- Name your bundle whatever you like (I do suggest not using available bundles). For example, I named mine Lightning
To make sure that your bundle is flexible in its development, might I suggest the following:
- Open Terminal
- Type “cd ~/Library/Application\ Support/TextMate/Bundles/(YourBundleName.tmbundle)/”
- Type “git init”
- -OR- import that stuff into Subversion
- -OR- make a repo in Mercurial
If you don’t know how to do the last 3 steps, or even what they mean, I’ve provided this handy link and a guided tour by my best friend mister sarcasm: here.
For my mad, mad purposes we will focus on one are today…
Snippets
Snippets are simply code snippets that take care of repetitive typing such as function names, HTML head tags, etc. They can either be keyboard shortcut based or “tab-activated”: if I type something like:
function
and hit tab, Textmate will kindly, graciously even, add my function snippet to the current document.
Snippets can be scope or context sensitive: if you’re typing in Python for example and you type “function” you won’t see a PHP function show up. When you add your first snippet, there is a text input at the bottom which asks for a scope selector. It’s good to set a scope for your own personal bundle, because you can have certain keywords like func + tab build a function in various languages to your preference: multiple snippets under one tab-activated word of magical-ness. To view your current scope in any language, type “CTRL + SHIFT + P”. The main scope you’re looking for is source.php or source.python, source.ruby, plain.text etc.
Mommy, Mommy! I made a snippet!
To create your first snippet, do the following:
- Make sure your freshly minted bundle is selected
- Click on the plus arrow in the lower left corner and select “New Snippet”.
- Name your snippet something understandable, for example “PHP Function Comment.” That’s pretty damn clear.
- You should see a window with a lot of helpful information in it. For our purposes, DESTROY that information
- Where it says scope selector type “source.php” since we’re writing for php (source.python if you’re in Python, source.ruby in Ruby, etc.).
Here is how our first snippet will look:
/**======================================
* ${1:undocumented function}
*
* @param $2
* @return ${3:void}
*======================================*/
It’s a PHP function comment! Keep your pants on, please.
The “$ + number” combination (e.g. $1) are called tab-triggers. This means that based on the order of the numbers, hitting tab will jump you to that area. You can then quickly fill out text, hit tab, and jump to the next area. How mind-blowing is that you assholes! This snippet will start the cursor in the top level function description with a default text of “undocumented function”.
The ${1:undocumented function} is a default placeholder. When you use the snippet it will appear as “undocumented function” without the brackets, and be selected. You can then type your funky function description and hit tab which will move you the @param area. If you just hit tab it will remain an “undocumented function”. This would make myself, and the small, hungry impoverished starving children of the world, very sad.
So the notation for making a default value appear is ${number: default value}, while the notation for a blank tab space is $number (e.g. $2).
But wait, there’s more…
You can have duplicate numbers, and when you type into one area (the first time the number appears in the list) it will duplicate itself.
For example:
/*
@class ${1:Class Name}
*/
public function $1 {
}
Try creating a new snippet, add a tab trigger word (I used “test”), don’t set a scope. Go into your editing window and activate your snippet (again by typing test and hitting tab) then begin typing. Watch as terrorists begin to take over your computers input duplicating the text. If that’s un-American then you can ship me to the caribbean.
But wait, there’s more…
Want to interchange between a string or a variable for a parameter snippet? Try this:
private ${3:"${4:value}"}
Activate your tab trigger and find everything in the quotes AND the quotes selected. Now you can just type a variable name. What if it’s just a string? Hit tab again and you’re automatically between quotes.
But wait, there’s more…1
Regular Expressions. Of which I need to spend time learning. Damn it. Essentially though you can run entered snippet data through a regex and reformat it. It all looks very cool on paper. The book talks about it and it looks really cool. I’m serious! If you look in the book you’re all like… “Woah” ...and… “Dude!”.
But wait, there’s more…
`date +%m.%d.%Y`
and your snippet just ran a shell command. Sweetness.
But wait, there’s probably more…
If you don’t get the point now, then you shouldn’t move forward. You should just quit, or consider reading some Plath and doing some cookin’.
See that wasn’t hard, you just didn’t try.
You can build complex snippet quite easily. To learn more about snippets, go here: http://manual.macromates.com/en/snippets
In our next Better series, sometimes in the ambiguous future deadline, we’ll talk about commands… just as soon as I master Ruby (Ruby, Ruby Ruby soho).
May you be flabbergasted.
1. I just finished Monkey Island: Special Edition. I love that game.
Integrity Section:
You can hire me for some sweet freelance. I'll also make fun of you for $10.

