#25 Custom Live Templates

PhpStorm allows you to create your own live templates (code snippets) to optimise your workflows.

Open the settings dialog and head into Editor | Live Templates, you can see the available live templates grouped by language. To add a new template click the + (plus) button and select Live Template. Specify the abbreviation (the short bit of text you type that will be expanded to the full code snippet) and a description.

Then provide the full code snippet in the Template text field. You can include variables in the template in the format $<variable name>$, which will allow you to provide values when the template is expanded. PhpStorm recognises $END$ as a special variable indicating the final position of the cursor after the template has been expanded and values have been provided for all variables.

Next click the Define warning text to specify which language the template is for and optionally the context it is available in.

Now the template is ready to be used. Open a file and type the abbreviation that was specified earlier, then hit Tab to expand the template. The cursor will be positioned on the first variable, provide a value then hit Tab to keep moving through all available variables. The final position of the cursor will be the location of the $END$ variable.

Example Templates

Here are some example templates you could add to your own workflows:

test: Test Method

/** @test */
public function it_$NAME$()

dd: Dump and Die


bsinput: Bootstrap Form Input

<div class="form-group">
    <label for="$ID$">$LABEL$</label>
    <input class="form-control" id="$ID$">

log: Log to Console


Further Reading

comments powered by Disqus