RSS Feed Subscribe to RSS Feed


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:

    public void $NAME$() {

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


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.


public void setup() {


public void tearDown() {


fail("Not yet implemented");


public void $NAME$() {


public static void main(String[] args){


Steps I took to add the IntelliJ settings to GitHub

First, I setup a new repo in GitHub at
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

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.

To use the settings files from GitHub with another IntelliJ installation

First, set up a couple of aliases, where IJS is short for IntelliJ Settings, and BU is short for backup:

export IJS=~/Library/Preferences/IntelliJIdea13

export IJSBU=~/Library/Preferences/IntelliJIdea13-BU

And before we do anything else, shutdown Intelli, then backup your existing IntelliJ settings:


Now, the main event is to clone the IntelliJ settings Git repo we set up earlier:

git clone $IJS

Finally, copy the original settings back in, but NOT replacing any files; that is, we lave the files we just pulled in from Git as is.

cp -r -n $IJSBU/* $IJS

Now, startup IntelliJ and you should have all the settings you checked in to Git.

Tags: , , , , , ,

2 Responses to “Live Templates in IntelliJ”

  1. Adam |

    Here are some others you might find useful:

    logger ->
    private static final Logger LOG = LoggerFactory.getLogger(YourClass.class);

    debug ->
    if (LOG.isDebugEnabled()) {

  2. sabram |

    Thanks for the suggestions Adam!

    I would go further and use this:
    logger ->
    private static final
    Logger logger = LoggerFactory.getLogger($CLASS_NAME$.class);
    When you are entering this, click “Edit Variables” and enter this into Expression:
    Also check ‘Skip if defined’.
    Now logger[tab] will insert the declaration with your classname already entered 🙂
    See more on this at

    I even like to setup a template for the imports, since there are so many Logger and LoggerFactory classes out there:
    loggeri ->
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    And, yep agreed on the debug template.
    Although in this case the LOG.isDebugEnabled() is definitely not required. isDebugEnabled() is only needed if the debug string would be computationally expensive. See

Leave a Reply