Search code examples
phphtmlphpstormkeymapping

Is there any shortcut key or menu exists to add php comment block to a php-html individual or mixed block in PhpStorm?


Before comment:

<h1>Hello</h1>
<h2><?php echo $world; ?></h2>

After comment:

<?php /* ?>
<h1>Hello</h1>
<h2><?php echo $world; ?></h2>
<?php */ ?>

Solution

  • There is no such special commenting style available.

    Based on your sample code: you can use custom Live Template of the surround kind to wrap your selected lines with opening and closing blocks (it will not add any extra commenting/escaping inside etc).

    1. Write such template first
    2. Once ready: use it.

    To create custom Live Template (official docs: https://www.jetbrains.com/help/phpstorm/using-live-templates.html):

    1. Go to Settings / Preferences | Editor | Live Templates
    2. Create a group where you will be storing your templates or use existing one
    3. Create new Live Template: give it an abbreviation (quick name), description (what this is about), actual template body and applicable contexts and some possible options.

      The body would be:

      <?php /* ?>
      $SELECTION$
      <?php */ ?>
      

      enter image description here


    How to use it:

    1. Select your code
    2. Invoke Code | Surround with... (check and use the shortcut that you have got there: it depends on your current Keymap) and use the right one.

    enter image description here