IntelliJ IDEA 2016.2 Help

File and Code Templates

In this part:

Overview

File templates are specifications of the contents to be generated when creating a new file. They let you create the source files that already contain some initial code.

You can view, edit and create the templates on the File and Code Templates page of the Settings/ Preferences dialog.

File and code templates are written in the Velocity Template Language (VTL).

So they may include:

  • Fixed text (markup, code, comments, etc.). In a file based on a template, the fixed text is used literally, as-is.
  • File template variables. When creating a file, the variables are replaced with their values.
  • #parse directives to include other templates defined in the Includes tab on the File and Code Templates page of the Settings/Preferences dialog box.
  • Other VTL constructs.

Here is a typical template example. (This template is used for creating a Java class.)

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { }

In this template:

  • ${PACKAGE_NAME} and ${NAME} are template variables.
  • The #parse directive is used to include the other template File Header.java.
  • The first line contains an #if VTL directive.

Applying this template leads to generating a file whose contents look similar to this:

package demo; /** * Created by IntelliJ IDEA. * User: John.Smith * Date: 6/1/11 * Time: 12:54 PM * To change this template use File | Settings | File and Code Templates. */ public class Demo { }

Per-project vs default scheme

IntelliJ IDEA suggests using file and code templates on the project or default (global) level.

If you need a sharable set of file and code templates, then these templates should be per-project; otherwise the templates are global and pertain to the entire workspace.

The file and code templates are stored in the following locations:

  • The default (global) templates are stored in the IntelliJ IDEA home directory, in the folder config | fileTemplates.
  • The per-project file and code templates are stored in the .idea | fileTemplates folder. These templates can be shared among the team members.

Refer to the section Project and IDE Settings to learn where the settings are stored, and to the File and Code Templates dialog for the description of the Schema field.

Predefined, internal, and custom templates

IntelliJ IDEA comes with a set of predefined file and code templates. You can use these templates as-is or modify them as necessary. You can as well create your own templates (custom templates).

Internal file and code templates are a subset of the predefined templates. These templates differ from all the other templates in that they cannot be deleted.

On the File and Code Templates page of the Settings/Preferences dialog, the names of internal templates are shown in bold. The names of the custom templates and the predefined templates that you have modified are shown in blue.

When are file and code templates used?

Whenever you create a new file, you can choose to create an empty file (e.g. File | New | File) or use a file template. In the latter case, the initial contents of the new file will be generated according to the template you have selected. (Basically, all the options in the New menu except File , Package and Directory correspond to using a template.)

menu_new

Are the choices of templates context-sensitive?

Generally, the set of the templates you can choose from when creating a new file depends on your module nature and configuration as well as the properties of your current location in the Project tool window. For example, you are not offered to use a template for an ActionScript class or interface, or an MXML component if your module is not a Flash module. Similarly, you cannot choose to create a Java class, interface, etc. outside of Java source or test directories (roots).

However, there are many templates that are available in any context.

See Also

Last modified: 23 November 2016