IntelliJ IDEA 2021.1 Help

File templates

File templates are specifications of the default contents for new files that you create. Depending on the type of file you are creating, templates provide initial code and formatting expected in all files of that type (according to industry or language standards, your corporate policy, or for other reasons). For example, this can be boilerplate code, meta information about the author, and so on.

IntelliJ IDEA provides predefined templates for all supported file types suggested when you create a new file. The set of suggested file types depends on the module and configuration, as well as the properties of your current location in the Project tool window. For example, IntelliJ IDEA will not suggest creating a Java class file outside of the Java source or test directories. For more information, see Content roots.

For example, if you right-click a directory in the Project tool window and select New, you will see a list of files that you can create in this context. These files have corresponding file templates configured in the settings.

List of file templates when creating a new file

To manage and configure file templates, open the Editor | File and Code Templates page of the IDE settings Ctrl+Alt+S.

  • The Default scope controls templates that apply to the entire workspace in any project. They are stored in the IDE configuration directory under fileTemplates.

  • The Project scope controls templates that apply only to the current project. They are stored in the project folder under .idea/fileTemplates. These templates can be shared among team members.

By default, the list of templates contains only predefined templates provided by IntelliJ IDEA. Some of them are internal, which means they cannot be deleted or renamed. The names of internal templates are shown in bold. The names of templates that you modified, as well as custom templates that you created manually, are shown in blue.

IntelliJ IDEA uses several types of templates, separated into the following tabs:

  • The Files tab contains file templates for creating new files.

  • The Includes tab contains pieces of reusable content for inserting into file templates (for example, if a specific header applies to multiple file templates).

  • The Code tab contains internal templates for code fragments (snippets) used by IntelliJ IDEA to generate various constructs. You can edit the available snippets on this tab, but you cannot create new ones.

    To create custom code snippets, use Live templates.

  • The Other tab contains templates used by various application servers and frameworks. You can edit existing built-in templates on this tab, but not create new ones.

The following procedures describe how to create file templates. Similar procedures can be used for creating include templates.

Create a new file template

  1. Press Ctrl+Alt+S to open IDE settings and select Editor | File and Code Templates.

  2. On the Files tab, click the Create Template button and specify the name, file extension, and body of the template.

  3. Apply the changes and close the dialog.

Copy an existing file template

  1. Press Ctrl+Alt+S to open IDE settings and select Editor | File and Code Templates.

  2. On the Files tab, click the Copy Template button and modify the name, file extension, and body of the template as necessary.

  3. Apply the changes and close the dialog.

Save a file as a template

  1. Open a file in the editor.

  2. Choose Tools | Save File as Template from the menu.

  3. In the Save File as Template dialog, specify the new template name and edit the body, if necessary.

  4. Apply the changes and close the dialog.


File and code templates use the Velocity Template Language (VTL), which includes the following constructs:

  • Fixed text (markup, code, comments, and so on), which is rendered as-is.

  • Variables, which are replaced by their values.

  • Various directives, including #parse, #set, #if, and others.

Fore more information, see the VTL reference guide.

The following example shows the default template for creating a Java class in IntelliJ IDEA:

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

In this template:

  • The #if directive checks whether the package name is not empty, and if so, adds the name to the package statement passed as the ${PACKAGE_NAME} variable.

  • The #parse directive inserts the contents of another template named File

  • Then the template declares a public class with the name passed as the ${NAME} variable (name of the new file).

When you create a new Java file, this template generates a file with contents similar to the following:

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 { }
Last modified: 14 May 2021