Timesavers: CFEclipse Snippets

Wednesday, April 29, 2009

This is part of an ongoing series on Timesavers. The goal is simple: short, easily-digestible posts designed to help developers get faster and more productive

Of all CFEclipse's features, none save me as much time as "Snippets". Eclipse has a built-in snippets feature, but for my part, I think CFEclipse's implementation is superior.

You create snippets in the Snip Tree view. You give it a name, a "start block", and optional "end block", and a "trigger". In addition, you can use "variables" to have the snippet inserter prompt you for stuff. More on that later.

First, here's what the dialog looks like for creating/editing a snippet:


Trigger Text

For me, its killer timesaver is trigger text. I don’t have to open up the snip tree view, navigate through a tree of snippets, and click to insert. Just type the trigger text and hit Ctrl-J.

For example, I have a snippet for creating a new CFFUNCTION. In a cfc, I simply type: function and then hit Ctrl-J. Here's what my snippet looks like:


And here's the popup I get when I hit Ctrl-J


I assign triggers to all the snippets I use on a day to day basis. I do have a number of snippets I use rarely, but I keep them as snippets because I figure if I have to type it more than once, it’s worth being in a snippet. But since I rarely use them, I’d forget the snippet trigger anyway.

If you were to chop out the trigger functionality tomorrow, I could measure my productivity loss in hours over the course of a year. And I’m not a fan of losing hours.


Variables are placeholders for text that you will be prompted to enter when you insert a snippet. For example, in that function snippet above, you saw this:

cffunction name="$${Function Name}" output="false" access="$${Access:public|private|package|remote}" returntype="$${Return Type:any|void|string|numeric|struct|array|query}" hint="$${Hint}"

And then when I inserted the snippet, it gave me a dialog where I'd populate those placeholders. If you don't enter anything, it just enters an empty string instead. Note also that you can create a dropdown list by using a pipe-delimited list, as in the "returntype" attribute above. Finally, if you use the same variable twice in a snippet, it will only prompt you for it once and then it will insert it in each place it was requested.

CFEclipse comes with several useful built-in variables you can use, too. They can be found at the official documentation site: http://trac.cfeclipse.org/cfeclipse/wiki/CFSnippets

You can watch Mark Drew’s video on using snippets here: http://cfeclipse.org/assets/movies/07_Snippetsfla.html

Finally, if you’re interested, here are some of the snippets I use every day. Many of these were created by my former colleague and all around good egg, Mike Rankin.

Bottom line: Next time you find yourself typing code that you've typed more than once, stop. Add it as a snippet, assign it trigger text, and execute the snippet by typing your trigger text then hitting Ctrl-J


Bradley said...


Snippets Rock

Seth Bienek said...

This is solid gold, thanks for sharing!

Dominic O'Connor said...

I am a snippet/keyboard shortcut junkie. I don't mean to shill, but I blogged a few weeks ago about combining CFEclipse snippets with AutoHotkey for Windows.

Marc Esher said...

Dominic, that's the second time in a few weeks someone's mentioned AHC to me, and seeing your post, I finally "get" it. Thanks a lot for sharing.

Is your blog aggregated by coldfusionbloggers.org or the goog? I can't believe I missed your post if it is indeed aggregated there.

Dominic O'Connor said...

Hi Marc,

Thanks, glad it helped. It is aggregated on coldfusionbloggers.org, but at least one of my posts never showed up in the feed, so maybe something wonky is going on.


Shaun McCran said...

Top article! I have used snippets in the past and always thought they were handy. I've never used the trigger text functions that you wrote about, they are even more of a time saver, especially as you can dynamically populate them. Its really fleshed out the benefit of snippets for me.

Marc Esher said...

Shaun, glad to hear it!

Al said...

The URL for the CFSnippets page in the wiki is actually http://trac.cfeclipse.org/wiki/CFSnippets

Grendell said...

Love CFEclipse snippets too, only thing I would like to see is the capability to high light text and have the snippet wrap it’s self around the high lighted code.


Michael Hnat said...

Unfortunately the links to the snippets are out of date. May you have actual links??

Marc Esher said...

Michael, here's the link for Snippets from the CFEclipse site: http://trac.cfeclipse.org/wiki/CFSnippets