• Share this article:

Code Template Expansion in Eclipse

Monday, February 27, 2006 - 19:23 by Wayne Beaton

Eclipse is great for lazy folks like me. By typing just a few keystrokes and hitting “ctrl-space”, Eclipse will generate a lot of code for you. For example, if you open a Java editor, move your cursor to a blank line and type “sysout”, it will expand what you’ve typed into System.out.println() and conveniently put the cursor between the parentheses.

I tend to use “toarray” a lot. This one is handy for converting a collection type into an array; it changes “toarray” into (type[]) collection.toArray(new type[collection.size()]). Even better, it put focus over the replaceable bits in the template and lets you tab through them. At first, it will put focus on “type”; anything you type there will appear in both places the word “type” appears. When you hit tab, it will move focus to “collection”. Very handy when you’re up against Java weirdness (you have to admit that the mechanism for converting a collection into an array is just weird looking).

There are a lot more templates. You can see the list that applies to the Java editors in the preferences dialog (Java > Editor > Templates).


Go ahead. Be lazy. You’re in good company.