A Guide to Eclipse Templates

By Rob Gravelle

https://www.htmlgoodies.com/beyond/java/a-guide-to-eclipse-templates.html (Back to article)

A Guide to Eclipse Templates

Templates are stored blocks of code or text that can be accessed quickly via a key combination. They can save you a lot of time when coding by eliminating the need to type the same lines of code over and over again. Eclipse already has a whole bunch of predefined ones that you can start using right away. All of these can be edited and deleted. Best of all, you can also create new ones to suit your purposes. In today's article's we'll be learning how to use Eclipse's predefined templates as well as how to create our own.

Viewing the Predefined Templates

To see what templates are defined in Eclipse:

  1. Open the Preferences dialog by selecting Windows > Preferences from the main menu.
  2. On the navigation tree on the left, go to Java > Editor > Templates.

You will see a list of pre-defined templates:

eclipse_templates_dialog (206K)

Some Useful Options

Automatic Insert

To insert a code block into the editor, type in the name of the code block and then hit the CTRL+SPACE key combination to activate the Eclipse code assist menu. In some instances, that will immediately insert the code; in others, a popup menu will appear from which you may select an option - the code block is the first entry, so just pressing ENTER will insert it.

Let's try the pre-defined sysout template; it expands to System.out.println(); when you type sysout followed by CTRL+SPACE.

On the other hand, if you type do followed by CTRL+SPACE, you'll get a context menu:

do_while_autocomplete (39K)

The reason for the differing behavior can be found in the Edit Template dialog. If you select the sysout item from the templates list on the Templates dialog and click the Edit... button, you can see some additional properties on the Edit Template dialog. One of these is the Automatically insert checkbox. Checking this box will cause the code to be inserted immediately when the template name is entered, followed by the CTRL+SPACE key combination. Note that it is checked for the sysout template:

edit_template_dialog_sysout (204K)

On the Edit Template dialog for the do template, the Automatically insert checkbox is not checked. That's why a context menu appears. If you don't want to see the context menu, just check the box, and see what happens!

edit_template_dialog_do (204K)

Use Code Formatter

Another option to be aware of is the Use code formatter checkbox on the Templates screen. This is a global option, so it affects all templates. Hence, clicking it causes all templates to be formatted according to the code formatting rules specified in the Code Formatter preferences. Leaving it unchecked has the effect of inserting templates with correct indentation and formatted as in the preview.

FYI, the Code Formatter preferences can be located from the Java > Code Style > Formatter node on the Preferences navigation tree. From there, click the Edit... button beside the Active profile to see all the various setting for such constructs as indentations, braces, white space, and others:

formatter_profile_dialog (261K)

Creating Your Own Template

Editing pre-defined templates is all well and good, but the real fun if you ask me is in creating your own templates. It's really quite easy. Click the New... button in the top-right corner of the Templates screen. On the New Template dialog, you can give your template a name, description and the code, called a pattern. Taking our previous example of the do loop, let's say that we wanted to create a different pattern that increments a counter. Here's how to define one:

It's a shame that there's no Save as command so that we could easily build on the existing do code block. Since there isn't one, you can always open the Edit dialog and copy the code that's there. In the New Template dialog, we'll assign the same name to our template - "do". There is no requirement that names be unique. Enter "do loop with counter" in the Description field. This is the text that will appear in the context menu. Make sure that the "Automatically insert" box is not checked or you'll never get to select between this and the regular do loop! Once you paste the existing do loop code into the Pattern box, we're ready to make it our own.

The first thing we need to do is define our counter variable. To do that, type the "int" type, a space, and click the Insert Variable... button at the bottom of the dialog. Of course there are no guarantees that the type of variable we're looking for has already been defined, but, with a little diligence - and luck - we might be able to find what we're looking for. Turns out that the index item does just what we need, so select it and hit Enter to insert it:

new_template_dialog_insert_variable_menu (88K)

To finish the declaration, type " = 0;" after the inserted "${index}" variable.

To increment the index variable, add a new line before the while statement, select the index variable from the list again, and append the "++" incrementor operator, followed by the semi-colon line terminator (;). The code should now look as follows:

int ${index} = 0;
do {
} while (${condition:var(boolean)});

Click the OK button on the Edit Template dialog and again on the Preferences screen to return to the code editor.

Open a Java file in the editor, enter the "do" keyword, along with the CTRL+SPACE code assist activator combination. Our template will now appear in the context menu. Select it to insert the code at the cursor location:

do_while_with_variable_autocomplete (38K)

By default the letter i will be used, but the cursor will be positioned so that you can immediately change it to something more to your liking. Moreover, you can tab between the counter variable, cursor location inside the loop, and the condition:

do_while_with_variable_code_in_editor (6K)


Eclipse makes it easy to use predefined templates as well as create our own. When you have some time, I suggest that you look over the list of exisitng templates. That will save you a lot of coding time down the road, as well as minimize the possibility of rewriting something that's already there!