Code Template Expansion in Eclipse

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.

This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Code Template Expansion in Eclipse

  1. Julia says:

    Being lazy can be a great thing. Until now, I was too lazy to create a blogger username just to post replies to your blog. (All other friends with blogger accounts allowed me to type in my name/URL.)Finally, because I wanted to tell you just how great this entry is, I created an account.Please keep writing about useful eclipse tips like those in this post. It helps me continue to be lazy and just read RSS feeds and not have to look up every last eclipse power-user feature. 😉

  2. Wayne says:

    Nice to hear from you Julia. I’m glad you liked it.

  3. Neil says:

    My favourite completions are:- “sysout” becomes System.out.println();- “inst” becomes:if(object instanceof Thing) { Thing thing = (Thing) object; …}

  4. Scott Stanchfield says:

    I like to go a step further…When defining template names, pick letter combinations that don’t start words in the java libs. That way, pressing control-space instantly drops in the suggestion.For example, rename “sysout” to “sop”. Nothing in the java libs starts with “sop”, so typing it and pressing control-space instantly drops in the evil System.out.println.– Scott

  5. almast says:

    If you have or know of collection of such templetes (exported in XML) please share

Leave a comment