7

When you have an HTML tag and you want to wrap it into another HTML tag, what is the fastest/easiest way to do so? Since it happens quite often, I'm looking for a way to optimize it.

Currently, it involves the following steps:

  1. Creating the new HTML tag right before the original tag
  2. Moving the (automatically generated) closing tag at the end of the original tag
  3. Selecting the original tag (can require page scrolling)
  4. Adjust indention on the inserted tag
  5. (Finally) done...

I'm looking for something like this:

  1. Selecting a (collapsed) HTML tag
  2. Pressing some shortcut (e.g. CTRL + B for <b>, CTRL + D for <div>) to wrap it into a specific tag
5
  • 1
    I should emphasize that using <b> is discouraged nowadays
    – Gantendo
    Commented Apr 28, 2023 at 8:51
  • 1
    Why not cut whatever you want wrapped, then type <container>, let it autocomplete and paste
    – Gantendo
    Commented Apr 28, 2023 at 8:52
  • stackoverflow.com/questions/40155875/… Maybe try WebStorm or Sublime?
    – Gantendo
    Commented Apr 28, 2023 at 8:57
  • @Gantendo Yes, that would be the better approach actually, it's just an old habbit I got from the DreamWeaver days (the shortcut worked there) and now it's in my muscle memory ;) Commented May 1, 2023 at 6:31
  • You can try extensions like 'WrapIt' for easier use:) Commented Feb 15 at 7:36

2 Answers 2

7

I don't know if there's an extension that does your exact 2 steps, but here's a very way to do it in 3:

  1. Select a HTML tag, collapsed or not - doesn't matter.
  2. Press hotkey for editor.emmet.action.wrapWithAbbreviation command. By default it's unset, so you need to configure it.
  3. Enter the tag that will wrap what you selected.

A way to do it in 2 steps could be with help of snippets, perhaps. You could create a bunch of snippets for different tags, like:

"Wrap with div": {
    "scope": "html",
    "prefix": "wrap",
    "body": "<div>$TM_SELECTED_TEXT</div>"
}

And then create a bunch of hotkey macros for first selecting the whole tag (e.g. editor.emmet.action.balanceOut) and then calling each of your snippet called with editor.action.insertSnippet and these args: { "name": "Wrap with div" }. See e.g. this answer for examples of defining macros.

3

So there is a way to wrap an HTML element easily with just 2 steps (after installing a vs Code extension):

So first Go into your VS code and install the extension called: "htmltagwrap".

Then you can do the magic:

1) Select the block of code you want to wrap, and -

If you are using windows press: Alt + W.

If you are using Mac press: option + W.

Then VS Code (and if I am not mistaken it is Emmet that-) will wrap the selected HTML element with a <p></p> tag automatically, but don't worry :p

2) Then straight away type the element you want and it will replace it.

4
  • This doesn't answer the question.
    – Toto
    Commented Aug 28, 2023 at 12:21
  • please elaborate how it doesn't answer
    – Johnny
    Commented Aug 29, 2023 at 17:33
  • They don't want to wrap only with <p></p> but with any tag.
    – Toto
    Commented Aug 29, 2023 at 18:52
  • With all due respect sir I thought I gave a solution in the second step, which you can type any HTML tag you want. It is a solution to the problem since it makes it possible to wrap any piece of code with any tag you choose. All you have to do is just type the HTML tag name. Please let me know what you think :)
    – Johnny
    Commented Aug 30, 2023 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .