Monday, September 17, 2012

Sublime Text 2 Snippets

So at work recently, I began using Sublime Text 2 for my ColdFusion and JavaScript programming. So far, I really dig the way it's both developer-focused and simple. For example, to edit your preferences, you modify a JSON file. See what I mean? None of those crazy properties pages. Just simple. If you're a developer.

Anyway, one thing I missed quickly from ColdFusion Builder / Eclipse was all my handy dandy snippets. You know, type a character or two, hit Ctrl+J and BAM! you've got the outline for a class or function. Or whatever you want, really.

Well, when I tried adding my snippets to Sublime, I had a little trouble getting them up and running. So here's how you do it!

First, from the Tools menu, choose "New Snippet..." This creates a new XML file in your editor. Here's an example of what the file looks like:

Now, save the file in your User package directory. For example, on a Mac that might be /Library/Application Support/Sublime Text 2/Packages/User. Luckily, Sublime Text is smart enough to point the Save dialog box to that directory when you try to save the snippet.

Unluckily, it doesn't give you any indication what you're supposed to name the file, and that's where I hit a snag. So if you're having trouble getting your snippets to work, listen up.

You have to name your file with a .sublime-snippet file extension. So, for example, I could name that snippet above something like cfinclude.sublime-snippet. If you don't put that file extension on there, it won't pick up your file, and your snippet won't work.

Now that it's saved, you can trigger your snippet by typing the contents of the <tabTrigger> element. So for example, using my snippet above, I can type inc and press Tab, and it'll write out <cfinclude template="filename" />.

Notice also the ${1:filename} bit. Doing that creates a field in your snippet. In the case of my example above, it writes the word filename inside the template attribute of my new cfinclude element, and highlights that text, so that I can fill it in with the filename I want to include.

I'm still getting used to Sublime Text 2, and I'm sure I'll discover some other goodies about it. Meanwhile, I'm moving a whole lot faster now that I've got my snippets loaded in!

No comments:

Post a Comment

Profile Picture
Dave Leeds
My Hobbies:
  • Programming
  • Cartooning
  • Music Writing
Full Profile