383

I would like to wrap my selected html within a tag in VS code. How do I do that?

Alex
  • 59,571
  • 22
  • 137
  • 126
hannes neukermans
  • 12,017
  • 7
  • 37
  • 56
  • 1
    One of the reason I'm unable to completely move to VSCode and ditch Sublime. – budji Oct 05 '18 at 17:30
  • 4
    @budji Its a built in feature with emmet... Pretty much anything you could want is also an extension now and if it isn't you can always make your own. – James Coyle Nov 07 '18 at 10:03
  • I like this question and I know and extension call (Emmet Wrap with Abbrevation), but anyone would know how to do the inverse actions. For example: write first

    ...

    and include inside ** with this result

    ...**...

    – Jmainol Apr 13 '19 at 13:36
  • You press tab after typing the tag name. E.g. "div[tab]". – Corey Alix Jan 18 '23 at 22:20

10 Answers10

743

Embedded Emmet could do the trick:

  1. Select text (optional)
  2. Open command palette (usually Ctrl+Shift+P)
  3. Execute Emmet: Wrap with Abbreviation
  4. Enter a tag div (or an abbreviation .wrapper>p)
  5. Hit Enter

Command can be assigned to a keybinding.

enter image description here


This thing even supports passing arguments:

{
    "key": "ctrl+shift+9",
    "command": "editor.emmet.action.wrapWithAbbreviation",
    "when": "editorHasSelection",
    "args": {
        "abbreviation": "span",
    },
},

Use it like this:

  • span.myCssClass
  • span#myCssId
  • b
  • b.myCssClass
Alex
  • 59,571
  • 22
  • 137
  • 126
150

A quick search on the VSCode marketplace: https://marketplace.visualstudio.com/items/bradgashler.htmltagwrap.

  1. Launch VS Code Quick Open (Ctrl+P)

  2. paste ext install htmltagwrap and enter

  3. select HTML

  4. press Alt + W (Option + W for Mac).

Barnee
  • 3,212
  • 8
  • 41
  • 53
hannes neukermans
  • 12,017
  • 7
  • 37
  • 56
  • You probably mean select Install at step 3. – Samir Sep 19 '17 at 12:34
  • 2
    "To use, select a chunk of code and press "Alt + W" ("Option + W" for Mac)." Simple enough! Except it's not working. I tried version 0.0.3 with VS Code 1.16.1. It *almost* works. It tries to wrap the selection in `

    ` tags, instead of the generic `
    ` which would seem more sane thing to do. What's worse is that it fails. It produces output like `

    My selected text.

    `
    – Samir Sep 19 '17 at 12:36
  • 1
    I tried other extensions like that. But so far I am not having any luck finding one that works. This one was closest to a working solution. I tried wrap 0.0.1 by David Taylor, and the Ctrl+i didn't work at all. – Samir Sep 19 '17 at 12:49
  • 2
    UPDATE: the extension now works correctly and addresses the above issues. No more duplicate tag, and you can now customize the tag you want to be inserted via a setting. So if you want the tag to be a `
    ` you add the following setting, `"htmltagwrap.tag": "div"`.
    – bgashler1 Oct 27 '17 at 17:58
  • that is pretty poor implementation - works for single tag, it would be nice to have something similar to notepad++ html plugin where you can wrap selection by tag from your configurable tag selection – Sasha Bond Aug 03 '18 at 20:14
  • I'm using it with VS Code 1.36.0. It's a bit clunky. If I wrap text with it and quickly add an attribute, the attribute name gets added to the closing tag, yuk! Code ends up like this: My link. The work around is to type the " – cakidnyc Jul 12 '19 at 01:57
  • This is quite good and better than the one I'm using in Atom (which I love but these little things may cause a switch). I hope this is part of the standard functions one day. I had never realized until recently how much tag wrapping I do at least in HTML. – cdsaenz Mar 03 '20 at 16:21
  • The extension was recently updated again and it should be more graceful. Handles single and multi-selection with arbitrary tag types (e.g. HTML, custom React components, etc...) – bgashler1 Apr 09 '23 at 04:26
89

As I can't comment, I'll expand on Alex's fantastic answer.

If you want the Sublime-like experience with wrapping, open up the Keyboard Shortcuts (command ⌘/Ctrl+shift+P > Preferences: Open Keyboard Shortcuts (JSON)) and add the following object:

{
    "key": "alt+w",
    "command": "editor.emmet.action.wrapWithAbbreviation",
    "when": "editorHasSelection && editorTextFocus"
}

Which will bind the Emmet wrap command to option ⌥/Alt+W when text is selected.

You can also use the UI to do this, open the Keyboard Shortcuts menu and search for "emmet wrap with abbreviation" to add the shortcut. enter image description here

Andrew Lewis
  • 1,251
  • 9
  • 16
  • 12
    This is how I have added shortcut: Preferences > Keyamp Shortcuts;... then search emmet wrap;... click + to add your shortcut;... :) – Luckylooke Apr 19 '18 at 10:22
  • 2
    additionally I added && resourceLangId == 'html' to work only for web pages – surpavan Dec 19 '19 at 20:03
  • 2
    @surpavan, nice but It can also be handy to have in other file types e.g. html template files – Andrew Lewis Dec 28 '19 at 03:05
  • 3
    @AndrewLewis - yes, that is why I kept it to language id and not file extension (files.associations). – surpavan Dec 28 '19 at 11:31
  • Shift+Alt+W is free for windows. – Timofeus Feb 27 '20 at 20:12
  • 1
    In command I had to write: editor.emmet.action.wrapWithAbbreviation instead of editor.emmet.action.wrapIndividualLinesWithAbbreviation to make it work. – Morfidon May 19 '21 at 11:53
  • @Morfidon It's worth noting that these, at least at the time of writing, are different actions and the experience in Sublime matched "Wrap Individual lines". The documentation is really vague about this now (https://docs.emmet.io/actions/wrap-with-abbreviation/ ) but I believe the actions have been merged. I'll check out wrapWithAbbreviation and update the answer if it has the same capability. – Andrew Lewis May 21 '21 at 11:05
59
  1. Open Keyboard Shortcuts by typing ⌘ Command+k ⌘ Command+s or Code > Preferences > Keyboard Shortcuts
  2. Type emmet wrap
  3. Click the plus sign to the left of "Emmet: Wrap with Abbreviation"
  4. Type ⌥ Option+w
  5. Press Enter
double-beep
  • 5,031
  • 17
  • 33
  • 41
Adam Gonzales
  • 829
  • 7
  • 11
17

imo there's a better answer for this using Snippets

Create a snippet with a definition like this:

"name_of_your_snippet": {
    "scope": "javascript,html",
    "prefix": "name_of_your_snippet",
    "body": "<${0:b}>$TM_SELECTED_TEXT</${0:b}>"
}

Then bind it to a key in keybindings.json E.g. like this:

{ 
    "key": "alt+w",
    "command": "editor.action.insertSnippet",
    "args": { "name": "name_of_your_snippet" }
}

I think this should give you exactly the same result as htmltagwrap but without having to install an extension.

It will insert tags around selected text, defaults to <b> tag & selects the tag so typing lets you change it.

If you want to use a different default tag just change the b in the body property of the snippet.

Unifex
  • 197
  • 1
  • 2
  • 1
    This is fine but Emmet can do this and much more, such as generating nested HTML, attributes and lists. Emmet comes out of the box with VS code. – Andrew Lewis Dec 28 '19 at 03:01
  • 1
    I don't understand the downvotes. Of course, Emmet is more complex and I can do a lot of things but for wrapping a text selection with a tag this solution is great! When you wrap, you don't have to move your eyes to the upper part of your screen/window. It happens right there, where you already were because you selected the text! It's a pretty common use case and quick solution with a low run time! – Andrei V May 07 '20 at 09:09
  • I was very excited with this however I could not make it work :/ – Icaro Jun 25 '20 at 02:50
  • The snippet written to file php.json has 'scope' word underlines, and tooltip is saying that 'property scope is not allowed'. Codium, version 1.60 – piotao Sep 29 '21 at 20:52
  • This works great! For those that want this to work in jsx/tsx, add `javascriptreact` and `typescriptreact` to the scope. – soundly_typed Mar 28 '23 at 05:00
6

With VSCode 1.47+ you can simply use OPT-w for this.

Utilizing built-in functionality to trigger emmet, this is the easiest way:

  1. Select your text/html.
  2. Shift+Option+w
  3. In the emmet window opened in the command palette, type in the tag or wrapping code you need.
  4. Enter
  5. Voila
rofrol
  • 14,438
  • 7
  • 79
  • 77
Tony Brasunas
  • 4,012
  • 3
  • 39
  • 51
4

Many commands are already attached to simple ctrl+[key], you can also do chorded keybinding like ctrl a+b.

(In case this is your first time reading about chorded keybindings: They work by not letting go of the ctrl key and pressing a second key after the first.)

I have my Emmet: Wrap with Abbreviation bound to ((ctrl) (w+a)).

In windows: File > Preferences > Keyboard Shortcuts ((ctrl) (k+s))> search for Wrap with Abbreviation > double-click > add your combonation.

2

I just installed htmltagwrap from extension marketplace and used ALT-W to wrap the tags (Windows Version).

1

There is a fast typing of the solution.

  1. Open the command palette (usually Ctrl+Shift+P)

  2. Preferences: Open Keyboard Shortcuts (JSON)

  3. Add this snap code

    {
        "key": "ctrl+`",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "~~${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}${TM_SELECTED_TEXT/^[\\t]*(.*)$/$1/}${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}~~"
        },
    }
    
  4. You select any text and press ctrl+`

result:

~~YourText~~
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

Incredible that the fastest solution still is Cut & Paste:

Cut selection (Ctrl+X/Cmd+X) -> Type tag -> Paste (Ctrl+V/Cmd+V)

One would have thought a shortcut would have been built in by now.

Janspeed
  • 2,644
  • 2
  • 22
  • 22