# Templates

The Templates preference page allows to create new and edit existing templates. A template is a convenience for the programmer to quickly insert often reoccurring source code patterns.

The following buttons allow manipulation and configuration of templates:

Action | Description
--- | ---
New... | Opens a dialog to create a new template.
Edit... | Opens a dialog to edit the currently selected template.
Remove | Removes all selected templates.
Import... | Imports templates from the file system.
Export... | Exports all selected templates to the file system.
Export All... | Exports all templates to the file system.
Enable All | Enables all templates.
Disable All | Disables all templates.
Use code formatter | If enabled, the template is formatted according to the code formatting rules specified in the Code Formatter preferences, prior to insertion. Otherwise, the template is inserted as is, but correctly indented.

## Template dialog

Creating a new template and editing an existing template uses the same dialog, which is described here.

The following fields and buttons appear in the dialog:

Option | Description
--- | ---
Name | The name of the template.
Context | The context determines where the template can be used and the set of available pre-defined template variables. Java The Java context Javadoc The Javadoc context
Automatically insert | If selected, code assist will automatically insert the template if it is the only proposal available at the caret position.
Description | A description of the template, which is displayed to the user when choosing the template.
Pattern | The template pattern.
Insert Variables... | Displays a list of pre-defined context specific variables.

## Template variables

Both Java and Javadoc context define the following variables:

Variable | Description
--- | ---
${cursor} | Specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place than to the end of the template on leaving template edit mode.
${date} | Evaluates to the current date.
${dollar} | Evaluates to the dollar symbol '$'. Alternatively, two dollars can be used: '$$'.
${enclosing_method} | Evaluates to the name of the enclosing name.
${enclosing_method_arguments} | Evaluates to a comma separated list of argument names of the enclosing method. This variable can be useful when generating log statements for many methods.
${enclosing_package} | Evaluates to the name of the enclosing package.
${enclosing_project} | Evaluates to the name of the enclosing project.
${enclosing_type} | Evaluates to the name of the enclosing type.
${file} | Evaluates to the name of the file.
${line_selection} | Evaluates to content of all currently selected lines.
${primary_type_name} | Evaluates to the name primary type of the current compilation unit.
${return_type} | Evaluates to the return type of the enclosing method.
${time} | Evaluates to the current time.
${user} | Evaluates to the user name.
${word_selection} | Evaluates to the content of the current text selection.
${year} | Evaluates to the current year.

The Java context additionally defines the following variables:

Variable | Description
--- | ---
${array} | Evaluates to a proposal for a declared array name.
${array_element} | Evaluates to a proposal for an element name of a declared array.
${array_type} | Evaluates to a proposal for the element type of a declared array.
${collection} | Evaluates to a proposal for a declared collection implementing `java.util.Collection`.
${index} | Evaluates to a proposal for an undeclared array index iterator.
${iterator} | Evaluates to a proposal for an undeclared collection iterator.
${iterable} | Evaluates to a proposal for a declared iterable name.
${iterable_element} | Evaluates to a proposal for an element name of a declared iterable.
${iterable_type} | Evaluates to a proposal for the element type of a declared iterable.
${todo} | Evaluates to a proposal for the currently specified default task tag.

Templates

Using templates
Writing your own templates

Java content assist
Task tag preferences
Code templates preferences
Code style preferences