I would like to wrap my selected html within a tag in VS code. How do I do that?
10 Answers
Embedded Emmet could do the trick:
- Select text (optional)
- Open command palette (usually Ctrl+Shift+P)
- Execute
Emmet: Wrap with Abbreviation
- Enter a tag
div
(or an abbreviation.wrapper>p
) - Hit Enter
Command can be assigned to a keybinding.
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

- 59,571
- 22
- 137
- 126
-
That color skin looks so eye-pleasing, wish I knew what skin and extension you've installed on your VS Code! – David Refoua Apr 21 '18 at 20:50
-
3https://marketplace.visualstudio.com/items?itemName=Equinusocio.vsc-material-theme – Alex Apr 22 '18 at 07:46
-
8
-
24
-
2For JSX, had to add this extra step: https://github.com/Microsoft/vscode/issues/4962#issuecomment-323880988 – Gezim Sep 17 '18 at 21:21
-
2With VSCode 1.35.0 (and possibly earlier) this works out the box with JSX - no need for any extra config – davnicwil Jun 18 '19 at 17:45
-
1That Emmet function does not appear in my VSCode (Windows VSCode v 1.43.1) What gives? – jpt Mar 20 '20 at 18:22
-
1May I ask where I could find the documentation for adding that vscode hotkey? (I couldn't find it on the vscode docs for the emmet...) – aderchox Mar 25 '20 at 16:22
-
lol wow u just opened a totally new door for me, there's a lot of Emmet commands i didn't even know ^^ – WtFudgE Jul 01 '20 at 07:59
-
-
1
A quick search on the VSCode marketplace: https://marketplace.visualstudio.com/items/bradgashler.htmltagwrap.
Launch VS Code Quick Open (Ctrl+P)
paste
ext install htmltagwrap
and enterselect HTML
press Alt + W (Option + W for Mac).

- 3,212
- 8
- 41
- 53

- 12,017
- 7
- 37
- 56
-
-
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 -
1I 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
-
2UPDATE: 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
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.

- 1,251
- 9
- 16
-
12This 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
-
2additionally 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
-
-
1In 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
- Open Keyboard Shortcuts by typing ⌘ Command+k ⌘ Command+s or
Code > Preferences > Keyboard Shortcuts
- Type
emmet wrap
- Click the plus sign to the left of "Emmet: Wrap with Abbreviation"
- Type ⌥ Option+w
- Press Enter

- 5,031
- 17
- 33
- 41

- 829
- 7
- 11
-
1This one should be the accepted answer as of 2022, straight to the point and you get the shortcut added, thanks! – Oscar Vasquez Jan 20 '22 at 15:45
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.

- 197
- 1
- 2
-
1This 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
-
1I 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
-
-
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
With VSCode 1.47+ you can simply use OPT-w for this.
Utilizing built-in functionality to trigger emmet, this is the easiest way:
- Select your text/html.
- Shift+Option+w
- In the
emmet
window opened in the command palette, type in the tag or wrapping code you need. - Enter
- Voila

- 14,438
- 7
- 79
- 77

- 4,012
- 3
- 39
- 51
-
7I've not found this to be the case for 1.47.3 on Mac OS. I had to add the keybinding for Opt+w as suggested by the other answers. – Mere Development Aug 06 '20 at 11:40
-
1Worked for me out of the box. Not sure why yours wouldn't. Perhaps you installed a plug-in that overwrote the shortcut? – Tony Brasunas Aug 06 '20 at 17:17
-
In my case it was shortcut conflict with `Csv to Json` plugin. – Krzysztof Przygoda Mar 09 '23 at 18:03
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.

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

- 31
- 4
There is a fast typing of the solution.
Open the command palette (usually Ctrl+Shift+P)
Preferences: Open Keyboard Shortcuts (JSON)
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/}~~" }, }
You select any text and press ctrl+`
result:
~~YourText~~

- 16,571
- 12
- 101
- 98
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.

- 2,644
- 2
- 22
- 22
...
and include inside ** with this result...**...
– Jmainol Apr 13 '19 at 13:36