123

Often in coding and templating I need to wrap a certain part of text. Is there any shortcut to wrap the current selection, for example:

Hello World
"Hello World"

Hello World
{{ trans 'Hello World' }}

Im using PhpStorm 7 for Mac and PC. I found something similiar, with: ctrl+alt+j you can wrap with a html-tag but nothing else. Also ctrl+alt+- comments the current selection according to the current file format(php, twig, html, ...)

Barnee
  • 3,212
  • 8
  • 41
  • 53
bench-o
  • 2,339
  • 5
  • 21
  • 21
  • 7
    **`Settings | Live Templates`** and use `$SELECTION$` -- should work for twig files as well. Once setup, use **Ctrl+Alt+J** to bring list of applicable templates. More details here: http://blog.jetbrains.com/webide/2012/10/high-speed-coding-with-custom-live-templates/ – LazyOne Dec 07 '13 at 01:30

4 Answers4

302

I know this is a little late answer, but I hope anyone who looks this question get helped.
In PhpStorm, almost everything can be configured but a little tricky and takes some learning curve.

It's very common behaviour that select a word and type single quote (or double quote) to surround it.

Hello World
'Hello World'
"Hello World"

To enable this, tick on checkbox of Preferences -> IDE -> Editor -> Smart Keys -> Surround Selection on typing quote or brace.

Another way, you select something (word, condition) and press Cmd + Alt + T (on Mac) , Ctrl + Alt + T (on Windows). Available Surround With options dialog box will be displayed.

Update (for PhpStorm8)

For PhpStorm Version 8, tick on checkbox of Preferences -> Editor -> General -> Smart Keys -> Surround selection on typing quote or brace.

Update (for PhpStorm 2016)

For PhpStorm Version 2016, use Cmd + Alt + S (on Mac) or Ctrl + Alt + S (on Win) to go to Settings. Tick on checkbox of Settings -> Editor -> General -> Smart Keys -> Surround selection on typing quote or brace`.

Barnee
  • 3,212
  • 8
  • 41
  • 53
Steve.NayLinAung
  • 5,086
  • 2
  • 25
  • 49
  • 34
    Update for phpstorm 8. File -> settings -> Editor -> Smartkeys -> Surround selection on typing quote or brace – Kiee Oct 01 '14 at 13:07
  • 5
    Why isn't this the default consistent with other popular editors out there. Beats me. – Aditya M P Mar 23 '15 at 07:29
  • 1
    Any version: Press CMD + Shift + A, search for Surround Selection, Press enter – lrepolho Sep 15 '16 at 01:57
  • I needed to use Ctrl + I to search through actions to find this instead of using search everywhere (configured to double tap shift) – thorne51 May 17 '17 at 07:48
26

As of 2015-02-26 (PhpStorm 8) go to:

File > Settings... > Editor > General > Smart Keys > Surround selection on typing quote or brace

Luke Watts
  • 457
  • 5
  • 13
14

How to create a live template to easily change Hello World into {{ trans 'Hello World' }}:

  1. Select something (word, condition) and press Cmd + Alt + T (on Mac) , Ctrl + Alt + T (on Windows). Available Surround With options dialog box will be displayed.
  2. Select Configure live templates.
  3. On the right bar there are a "+" to add either a group or live template.
  4. In Edit part choose "Abbreviation", so you could see it in a menu later.
  5. In "Template text" add following: {{ trans '$SELECTION$' }}
  6. Click ok to save changes.

After that select Hello World and use step 1 from above, select a rule with your "Abbreviation"

This is just a more detailed answer than @Steve.NayLinAung . Credit goes to @LazyOne

Barnee
  • 3,212
  • 8
  • 41
  • 53
Artem L
  • 10,123
  • 1
  • 20
  • 15
4

On a Mac, it's

PhpStorm > Preferences > Editor > General > Smart Keys > Surround selection on typing quote or brace.

You can also press command+, to open the Preferences menu.

Mladen Danic
  • 3,600
  • 2
  • 25
  • 24