CLion 2018.1 Help

File and Code Templates

File templates are specifications of the default contents to be generated when creating a new file. Depending on the type of file you are creating, templates provide initial code and formatting that is expected to be in all files of that type (according to industry standards, your corporate policy, or for other reasons).

CLion provides predefined templates for all supported file types, which are suggested when you are creating 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, CLion will not suggest to create a Java class file outside of the Java source or test directories.

File templates are managed on the File and Code Templates page of the Settings/Preferences dialog. This settings page can be configured in one of two scopes:

  • The Default scope controls templates which pertain to the entire workspace. They are stored in the IDE configuration directory under fileTemplates.

  • The Project scope controls templates on a per-project basis. 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 CLion. Some of them are internal, which means they cannot be deleted (also, you cannot change their names). Names of internal templates are shown in bold. Names of templates that you modified, as well as custom templates that you created manually, are shown in blue.

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

  • The Files tab contains file templates, which can be used for creating new files.
  • The Includes tab contains templates of reusable content that can be inserted into file templates (for example, as headers).
  • The Code tab contains internal templates for code fragments (snippets) used by CLion for generating various constructs. You can edit available snippets on this tab, but not create new ones.
  • 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.

To create a new file template:

  1. Open the Settings/Preferences dialog (Ctrl+Alt+S), click Editor and then File and Code Templates.
  2. On the Files tab, click Create Template (The Create Template button) and specify the name, file extension, and body of the template.
  3. Apply the changes and close the dialog box.

To copy an existing file template:

  1. Open the Settings/Preferences dialog (Ctrl+Alt+S), click Editor and then File and Code Templates.
  2. On the Files tab, click Copy Template (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 box.

To save a file as a template:

  1. Open the desired file in the editor.
  2. On the Tools menu, click Save File as Template.
  3. In the File and Code Templates dialog box that opens, specify the new template name and edit the body, if necessary.
  4. Apply the changes and close the dialog box.

Syntax

File and code templates are written in the Velocity Template Language (VTL), which enables using the following constructs:

  • Fixed text (markup, code, comments, etc.), 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 JavaScript class in CLion:

/** * Created by ${USER} on ${DATE} */

In this template, ${USER} and ${DATE} are template variables.

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

/** * Created by John.Smith on 6/7/11 */
Last modified: 24 July 2018