How to create a reusable code template in PHPStorm

JetBrains actually calls these Live Templates. They allow you to quickly insert a code template for any language and get prompted for filling optional variable names. The snippet is then bound to an abbreviation of your choice.

There’s so many settings in PHPStorm, I’m not surprised if you didn’t know about the enormous number of Live Templates available to you out-of-the-box. They’re activated via a short abbreviation, then Tab or Enter.

To access them, open the dialog at File > Settings > Editor > Live Templates. Here’s a list of the default categories:

PHPStorm built-in Live Templates

Just look at the collection of VueJS-based Live Templates available in PHPStorm. You really need a cheat sheet pasted to your wall with all of these shortcuts.

VueJs Live Templates in PHPStorm

To create your own custom Live Template or snippet, click the + icon in the upper right corner. You can create your own Group or place your snippet into an existing Group.

Adding new Live Template

Choosing Live Template reveals this form:

Enter an alpha-numeric abbreviation, fill out the optional description and paste in your code template. If you have a replacement field, like a class-name or other, you should surround them with dollar signs like $CLASSNAME$.

Below the template field is a selector for choosing the type of code file your template should be insert-able into. Choose any that apply to your snippet.

For this example I created a PHP template, but you can create a template in any language supported by PHPStorm. Here’s a visual of the Live Template in use:

Inserting a Live Template

It’s your turn! Go ahead and try creating your own live template or memorize some of the existing PHPStorm snippets to improve your coding efficiency.