73

I use PhpStorm. I want to make snippets of code and then assign shortcuts to them. I can do it in NetBeans, but in PhpStorm I don't know how to make them.

For example, to add console.log(); in NetBeans, I only type con + space. con is the shortcut that I had defined.

2 Answers 2

158

In IntelliJ (the Java IDE) or PhpStorm (the PHP IDE) it is called Live Templates.

Go to Settings and start typing "live templates" in the search box. On the right panel you can edit and create such templates.

So in your case, you simply type "con" in the abbreviation text box and "console.log();" in the template box text area below. You may even change, with which action you want to expand the template (Tab is default, space and enter are possible).

The live template thing is much more complex than what I wrote above. You can use variables and some context information. For further reading, I suggest the JetBrains / PhpStorm documentation.

Sign up to request clarification or add additional context in comments.

8 Comments

thank you very much. can you tell me how to change the cursor position?
I find out how. I use this console.log($s$); and then when the editor adds it the cursor moves to the position of $s$.
@AladdinHoms You can use $END$ to specify final position where cursor will end up after all variables (like your $s$) are processed/filled.
As I needed more info I went to PHPStorm's help area here
Remember to enable it for JavaScript. You do that by clicking Define in the text "No applicable contexts. Define" beneath the Template text box.
|
-2

Step 1: Open Live Template Settings

  • Windows/Linux: File > Settings > Editor > Live Templates

  • macOS: PhpStorm > Preferences > Editor > Live Templates


Step 2: Create a New Template

  1. Click the + button at the top-right.

  2. Select Live Template.

  3. Fill in:

    • Abbreviation: e.g., cpded

    • Description: What the snippet does (optional)

    • Template Text: Your actual code snippet


Step 3: Set Applicable Context

  1. Click Define next to “Applicable in…”

  2. Choose a suitable context (e.g., PHP, HTML, JavaScript, or a custom file type).

1 Comment

Hi Vatsal. Did you copy this answer from the output of an AI assistant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.