72

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.

Mary
  • 197
  • 1
  • 15
Aladdin Mhemed
  • 3,817
  • 9
  • 41
  • 56

1 Answers1

153

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.

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
synopia
  • 2,254
  • 1
  • 16
  • 4
  • thank you very much. can you tell me how to change the cursor position? – Aladdin Mhemed Mar 29 '12 at 05:26
  • What do you mean by changing the cursor position? At least in IntelliJ in the live template settings there is a checkbox to reformat, after the template has been expanded. Then the live template lines are indented. – synopia Mar 29 '12 at 05:29
  • 15
    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$. – Aladdin Mhemed Mar 29 '12 at 05:33
  • 30
    @AladdinHoms You can use `$END$` to specify final position where cursor will end up after all variables (like your `$s$`) are processed/filled. – LazyOne Mar 29 '12 at 13:26
  • 2
    As I needed more info I went to PHPStorm's help area [here](http://www.jetbrains.com/phpstorm/webhelp/creating-and-editing-live-templates.html) – rs77 Dec 29 '12 at 13:47
  • 7
    Remember to enable it for JavaScript. You do that by clicking `Define` in the text "No applicable contexts. Define" beneath the `Template text` box. – mlunoe Dec 10 '13 at 10:40
  • How can i define template `console.log` template for comma separated variables, i.e: selected text 'a, b' should be converted into `console.log(a);console.log(b);` – hellboy Dec 02 '15 at 15:40
  • On Mac, go to PhpStorm > Preferences > Live Templates - and follow the advice above (inc. enabling for the correct language) – Jono Sep 04 '21 at 09:40