Live Templates in IntelliJ

As described here, IntelliJ’s live templates let you easily insert predefined code fragments into your source code.

I have posted some of my most used templates below, a link to my complete list of template files on GitHub (as a reference for myself when I setup new IntelliJ environments) and the steps I took to add the IntelliJ settings file to GitHub.

For example, I set up a template such that I can type test, hit tab, and it will insert this JUnit code snippet for me:
 
 

@Test
public void $NAME$() {
    $END$
}

It is a JUnit test method, with the cursor initially placed after “public void”, ready for typing the test name. The cursor then jumps to between the {}s, ready to start writing the test.

IntelliJ templates are stored in a user.xml file at

~/Library/Preferences/<product name><version number>/templates

For example, for IntelliJ13, it is

~/Library/Preferences/IntelliJIdea13/templates/user.xml

Some of my other templates are listed below, with the trigger in bold.
So that I can use these templates on any IntelliJ (e.g. work and home), I have checked my complete list in here at GitHub.

before

@Before
public void setup() {
    $END$
}

after

@After
public void tearDown() {
    $END$
}

nyi

fail("Not yet implemented");

puv

public void $NAME$() {
    $END$
}

main

public static void main(String[] args){
    $END$
}

Steps I took to add the IntelliJ settings to GitHub

First, I setup a new repo in GitHub at https://github.com/sabram/IntelliJ
Then, I followed some instructions from this StackOverflow posting on How to convert existing non-empty directory into a Git working directory:

cd ~/Library/Preferences/IntelliJIdea13
git init
git add templates/user.xml
git commit -m 'initial version of IntelliJ user.xml'
git remote add myIntelliJRepo https://github.com/sabram/IntelliJ.git

At this point, I got an error suggesting I needed to do a git pull first. But when I did a

git pull saIntelliJ

I got an error saying

You asked to pull from the remote 'saIntelliJ', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

So, I edited .git/config based on this posting, to include

[branch "master"]
remote = saIntelliJ
merge = refs/heads/master

Then I was able to do

git pull saIntelliJ    
git push -u saIntelliJ master

successfully, and can just use git pull and git push going forward, with no need to specify the repo name (saIntelliJ) each time.

Reference: Live Templates in IntelliJ from our JCG partner Shaun Abram at the Shaun Abram’s blog blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Live Templates in IntelliJ"

  1. Wesslan says:

    Live Templates are extremely useful and productive!

Leave a Reply


2 × one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books