31

Suppose I have a text:

This is my text!

How beautiful it is!

And I want to wrap each line within a p-tag, is there an easier way than to navigate to each line and add them manually and have them close semi-automatically?

I want them to look like this:

<p>This is my text!</p>

<p>How beautiful it is!</p>

while having to do as less as possible.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • 1
    Of course, right after posting I find the relevant question: http://stackoverflow.com/questions/20435802/phpstorm-wrap-surround-selection The Cmd + Alt + T (on Mac) , Ctrl + Alt + T solution is what I was looking for. – k0pernikus Jun 19 '14 at 10:41
  • I'm pretty sure I saw this same question asked a day or two ago about WebStorm, but now I can't find it. – Barmar Jun 19 '14 at 10:42

1 Answers1

72
  1. Select your lines
  2. Code | Surround With...
    For Windows/Linux: Ctrl + Alt + T
    For MacOS: Cmd + Alt + T
  3. Choose "Emmet"
  4. Type p* -- this will surround each line with <p> tag

Screenshot of PhpStorm Emmet functionality

NOTE: Empty lines will be wrapped as well so it is better remove them in advance.

Similar/related case.


P.S.

In current versions of IDE the dedicated "Surround with Emmet" action is available which allows you to bring that popup window in one key stroke instead of having going trough via intermediate Surround with... popup menu first.

That action has no shortcut defined by default, but you can easily change that and assign any desired shortcut in Settings/Preferences | Keymap -- just look for Other | Surround with Emmet action (hint: use search box to speed up your search).

Community
  • 1
  • 1
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 1
    Is there a way that you know to make Emmet the default option? – Timidfriendly Dec 19 '14 at 14:13
  • @Timidfriendly What do you mean by "default option"? – LazyOne Dec 19 '14 at 14:44
  • The 'surround with' command currently brings up 3 options: 1. surround with tag. 2. surround with CDATA. 3. Emmet. I would like the first option to be Emmet. – Timidfriendly Dec 21 '14 at 07:47
  • @Timidfriendly That is not configurable. But you can press `e` to select that item in that list (instead of `Enter` for default one). – LazyOne Dec 21 '14 at 11:26
  • Btw is there a way to make it so you can create a shortcut which would wrap the text in tag of your choosing without actually typing the name of the tag? – Petar Vasilev May 05 '15 at 09:40
  • 1
    @PetarVasilev Look at [Live Templates](https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm#LiveTemplates%28Snippets%29inPhpStorm-CreatingSurroundTemplates) then ... or record a macro and assign custom shortcut to it. – LazyOne May 05 '15 at 09:47
  • Is it possible to do this and keep the carriage returns, so that it doesn't all appear on one line. Thanks – ejntaylor Jan 18 '18 at 17:34
  • @raison Screenshots/examples please. Right now I'm not sure what you mean. – LazyOne Jan 18 '18 at 17:43
  • @LazyOne Screenshot: https://www.dropbox.com/s/y13w0lsj9qn9580/Screenshot%202018-01-18%2021.12.06.png?dl=0 I would like it to process like this instead: https://www.dropbox.com/s/zn4wxptsnrlsib0/Screenshot%202018-01-18%2021.12.53.png?dl=0 – ejntaylor Jan 18 '18 at 21:12
  • @raison Show your Emmet settings. It works here. Right now it looks like you have `Single line` filter enabled. – LazyOne Jan 18 '18 at 22:25
  • Thanks for help @LazyOne -- PhpStorm Emmet Settings: https://www.dropbox.com/s/5i97ntv4yar2atb/Screenshot%202018-01-19%2012.50.34.png?dl=0 – ejntaylor Jan 19 '18 at 12:50
  • @raison Where did you try it? The context/file type. If it will be in empty HTML or PHP file -- yes, I see the same. But if it's inside the correct HTML (e.g. inside the `
    ` of a proper outer layout) -- it works fine. 1) https://postimg.org/image/5r6ocdtdh/ 2) https://postimg.org/image/hubzzsc3p/
    – LazyOne Jan 19 '18 at 13:02
  • Aha, that works. If I run it in an empty file then it is side by side. If I wrap it in a
    then it keeps the returns. Thanks for that. https://www.dropbox.com/s/869fmcnuxgvehqj/Screenshot%202018-01-19%2013.09.40.png?dl=0
    – ejntaylor Jan 19 '18 at 13:10
  • In Ubuntu 18 that key combination invokes the terminal. – isherwood Feb 07 '19 at 14:45
  • 1
    @isherwood Check the Keymap used in IDE -- maybe you are using one that is not compatible with Ubuntu. If it meant to be compatible (correct keymap is used) -- change the shortcut for that action to something else that will not conflict with Ubuntu's special shortcut (`Settings | Keymap`). – LazyOne Feb 07 '19 at 15:33
  • **Use ctrl+alt+j instead**. This is an old answer so maybe it didn't exist before, but for this particular action `ctrl` + `alt` + `j` is superior, since "surround with" is the first option in the menu that appears. And, ctrl+alt+t is way too difficult than is reasonable to input if you have a large ergonomic keyboard. – Skeets Feb 11 '22 at 00:55