How to tweak Eclipse templates to suit you

I discussed templates and why to use them in another post. Basically, we use them to eliminate the pesky, repetitive code zombies. I also showed some built-in templates and what they do.

But what if you want to add your own template or modify an existing one? Managing your own templates is important because your business domain and how you work determines to a large extent what is the common code you use all the time.

Don’t be tempted to say “It takes too long to create the template. In that time I could’ve typed the code myself.” Are you going to say that the next 50 times you have to type the same code? I always force myself to sacrifice the 2 minutes because I’ll save me 20 minutes in the long run.

Advertisement

How to add/edit a template

In the spirit of show, rather than tell, here’s a video that shows how to create a template to make it easier to add an additional case-statement to a switch-block. We want to add case blocks for IDLE and OFF for the enum EngineState.

A couple of notes on some of the fields we saw in the video:

  • Name: The shorter the better, because you want to type as little as possible to invoke it. But make it unique enough so you don’t get too many matches.
  • Context: Eclipse will only show the template in the list of available templates if it belongs to the right context. Eg. when editing JavaDoc’s, none of the Java statements templates will appear, only the Javadoc ones.
  • Description: Make it nice and… descriptive, stating what the template does. The short name may not always be enough to know this.
  • Pattern: You can mix normal Java code with template variables. I describe variables below.

Variables

You can use variables to make the template a bit smarter. Eclipse has a raft of built-in variables and although all variables are useful, some are more useful than others.

The example below (from the video) shows 2 variables: ${cursor} which tells Eclipse where to leave the cursor after cycling through all placeholders and ${value} which is a custom variable, ie. not built-in.

case ${value}:
    ${cursor}

    break;

Keep the following in mind when using templates:

  • You can use the Tab key to move from one variable to another. No mouse or arrow keys required.
  • Custom variables don’t mean anything to Eclipse but they can be used later in the same template to refer to the same user-provided name. See the new template for an example of this.
  • You can also use an unnamed variable (${}) to signify a placeholder where the user should enter something but the variable isn’t used anywhere else.
  • Other variables can tell Eclipse to guess which field to use or to get documentation info (eg. current author).
  • Variables must be enclosed in ${} – don’t forget this.

Useful Variables

Here’s a rundown of the most useful variables. You can get a full list by pressing Ctrl+Space while editing a template.:

  • cursor – Tells Eclipse where the cursor will finally end up after you’ve edited all the other variables. Leave the cursor somewhere sensible like on a blank line so you can start typing immediately.
  • var – A list of variables in scope, the first guess being the last variable declared in the smallest scope. You can pass a type of limit the list to only variables of that type.
  • iterable, array, collection - A list of either iterables, arrays or collections in scope, starting with the last declared variable in the smallest scope. Nice for loops over an array/collection. iterable is sufficient for most situations as it covers both arrays and collections.
  • line_selection – Perfect for templates that wrap a block of selected code in a loop or some other construct. The for and while templates use these to allow you select code that will be placed in the loop. To use, select the code, then press Ctrl+Space twice to get Template Proposals (you may need more presses depending on how you’ve set up your proposal options.
  • import – A specific import you need generated together with the template. For example, a template that generates a log4j statement can also import log4j’s Logger. The import’s ignored if it already exists.
  • newName – A unique name for a variable based on the type you pass it. Eg. if you pass the type MultipleDocHandler, a variable with the name multipleDocHandler will be generated.

How to import/export a template

To make it easy to share templates, Eclipse allows you export a template so that someone else can import them. An XML file is produced which contains the template(s) you selected. I’ll be sharing some handy home-grown templates as XML files.

Here’s how to import/export a template

  1. Go to Window > Preferences > Java > Editor > Templates
  2. Select the templates you want. NB! The checkboxes don’t indicate what’s selected; they are used to enable/disable a template. A template is selected if the whole row in the table is selected, so use Ctrl+Left Click or method specific to your OS to multi-select templates.
  3. Click Import… and choose the XML file you received. Or Export… and provide a filename.

Related Tips

Share This Tip

2 thoughts on “How to tweak Eclipse templates to suit you

  1. how to write text depends on class name
    example if class name is exampleactivity write activity
    and if class name is examplefragment write fragment

    • I haven’t seen a way to do that, shashidhar. I only know of the ${enclosing_type} variable that gives you the full class name but I don’t know how to get substrings from it. Maybe someone else can help?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>