ashish ashish - 4 years ago 235
Java Question

How can we write our own code template file in IntelliJ IDEA

I need to create a sample code template in my IntelliJ IDEA project so that everybody in the team can also kind of import it in their IDEs and use it.

I am able to do it on my own machine by changing the "class" template myself, but i just don't have any way to make it available to my team like exporting it to a file that can be used, in Eclipse it is possible to have one sampleCodeFormatter.xml file that everybody can import in their eclipse workspace.

How does the same thing works in IntelliJ IDEA?

Answer Source

First, in IntelliJ, open Settings (ctrl-alt-s), under IDE Settings find File and Code Templates and have a look at the Templates tab.

You will see some templates there already, for example there is one called Class:

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

Note that it includes File Header, which is found under the Includes tab.

Now, you can easily create your own file templates here. Under the Templates tab, just hit the green "Add" button, give your new template a name (Foo ?),ensure the extension is java and write a valid java class.

Save, and your newly configured template exists here:

C:\Users\{USER}\.IntelliJIdea12\config\fileTemplates\Foo.java

You should be able to share this file with your team.

A neat feature is that from within the Project pane of a java project, under a source root, hit alt-enter, choose a New Java Class and under the Kind drop-down - voila! You can see your Foo template as a valid option.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download