Custom Postfix Templates

Compatible with: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio
Nov 05, 2018
26 822
Documentation | Screencast | Predefined templates

This plugin lets you define your own postfix templates for Java, Scala, SQL, PHP, Kotlin, Python, Dart, JavaScript, Rust, and Groovy. It also comes with hundreds of useful postfix templates shared by the community. You're welcome to contribute your templates as well.

Apart from the community approach, how does the plugin differ from IDEA's postfix templates?

Since IDEA 2018 you are now able to define your own postfix templates for some languages in the settings UI (Editor → General → Postfix Templates). However, this is a pretty new feature and it's less functional than this plugin. Here are some of the advantages of this plugin:

  • You can define different template rules (context based) for the same template name, e.g. .toList should behave differently for arrays and for sets.
  • You can use template variables (e.g. $varName$) which are filled in by the user while applying the template.
  • You can use live template macros to automatically fill some of the template variables (e.g. $var:suggestVariableName()$) as well as you can define default values.
  • You can restrict the availability of templates or template rules to the availability of certain classes or libraries (e.g. expand "test".val to val s = "test" if Lombok is available).
  • It allows you to use static imports instead of class imports (e.g. array.toList can be expanded to asList(array) instead of Arrays.asList(array) if you add [USE_STATIC_IMPORTS] to the rule).
  • It comes with more than 200 useful and editable postfix templates for Java with more than 370 template rules, e.g.
    • string.toInt → Integer.parse(string)
    • array.toList → Arrays.asList(array)
    • file.lines → Files.readAllLines(file.toPath(), Charset.forName("UTF-8"))
    • file.getName().val → final String name = file.getName();
  • There are multiple ways to easily share your templates with others.

Download plugin

Recent change notes

Version 2.7.0
  • Added support for new Kotlin matching types: STRING_LITERAL, INT_LITERAL, FLOAT_LITERAL, CHAR_LITERAL.

General usage instructions

Go to Tools → Custom Postfix Templates and write your own postfix templates for your code or just the predefined ones to simplify your life. For instance, write "array.toList" and see how it gets replaced by "Arrays.asList(array)". Other predefined but editable templates are: toByte, toChar, toInt, toLong, toFloat, toDouble ● toList, toSet, toMap ● sort, sortBy, minBy, maxBy, groupBy ● exists, forall ● reverse, concat ● Optional.forEach ● String.r