Eclipse Tip: Add your own Code Template in Eclipse

If you are using Eclipse as your primary development IDE, then you may want to know about Code templates in Eclipse. Code templates are certain user defined (some are available by default) templates which can assist in rapid code writing. Not only code templates increase your code writing speed, but it also add consistency across the code.

To add a code template, all you need to do is to write first character and then press CTRL + SPACE. For example, in Eclipse write tr and press ctrl+space. It will add a try-catch block in your editor.

User defined code templates

You can defined your own code templates. Just goto Windows > Preferences > Java > Editor > Templates.
You can click on New.. button to add your own code template.
eclipse-code-templates

Lets create our own code template. For this example, we will create a template which will check if a local variable is null or not, if its null then it will throw a NullPointerException.

Open Template editor from above path and press New.. Then enter following details.
Name: npe
Description: To check if a local variable is null or not.
Pattern:

if (${arg:localVar} == null)
    throw new ${exception:link(NullPointerException,IllegalArgumentException)}("${arg:localVar} is null");

And press Ok to save the template.

Now go to any editor window in Eclipse and press npe and press CTRL + SPACE. You will see a suggestion window which will display you an option to select the npe code template.



9 Comments

  • newJavaDeveloper 11 April, 2011, 12:00

    This is great!..Thanx..

  • Gautam 4 April, 2012, 9:13

    Code Templates are very useful indeed.

  • vinayak 28 June, 2012, 22:11

    This is very usefull to me .i want learn more different kind of coding template

  • Hrushikesh 27 November, 2012, 2:52

    Great post !!! Thanks for sharing !!

  • shailesh 4 September, 2013, 13:19

    Hi,
    I have done with the above code. it works fine for me. But when i shift the workspace it is gone.
    How to persiste the templates in eclipse ide.

    I have definde a new shorcut key to do duplicate line code.(CTRL+L) and again .(CTRL+L)
    but it also not persiste in my new works space.

    How to do that
    is it possibe?

  • nitendra 18 November, 2013, 13:27

    I imported my template file but its not reflected.

  • Avishek 31 January, 2014, 10:41

    I have done with the above code. it works fine for me.Thank you…

  • Leena 11 February, 2014, 9:58

    I am using code templates. It is very useful. Try using Fast Code Templates.

  • sunil 1 April, 2014, 12:18

    i got difficult in that ,template not added succefullyt. please give step to add any template in eclipse.?

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current day month ye@r *