Reputation: 2779
This seems like the most basic question in the world, but damned if I can find an answer.
Is there a keyboard shortcut, either native to Visual Studio or through Code Rush or other third-party plug-in, to wrap the current selection with an HTML tag? I'm tired of typing the opening tag, cutting the misplaced closing tag to the clipboard, moving the cursor, and pasting it at the end where it belongs.
Update: This is how TextMate handles surrounding a selection with a tag. Frankly, I'm stunned that Visual Studio doesn't seem to have a similar feature. Creating a macro or snippet for every conceivable tag I might want to use seems absurd.
Upvotes: 125
Views: 58083
Reputation: 72
I know this is an old question but I was just struggling with the same thing. You can install the Emmet Keybindings extension by Andrés Gutiérrez. Once installed you can highlight text then use control + MW to wrap with any tag you'd like. To give each line an opening and closing tag include an * after the tag.
Upvotes: 0
Reputation: 2393
Visual Studio 2015 comes with a new shortcut, Shift+Alt+W, that wraps the current selection with a div. This shortcut leaves the text "div" selected, making it seamlessly changeable to any desired tag. This coupled with the automatic end tag replacement makes for a quick solution.
This shortcut is available in Visual Studio 2017 as well, but you must have the "ASP.NET and Web Development" workload installed.
Shift+Alt+W > p > Enter
Upvotes: 159
Reputation: 1410
For those who use Visual Studio 2017: Right click on an html
/cshtml
area, or select some elements to wrap, there is a Wrap With <div>
button on the list.
Upvotes: 11
Reputation: 813
If you have Web Essentials installed, you can use Shift+Alt+W to surround a selection with a tag.
Upvotes: 43
Reputation: 2814
Ctrl-X -> Type tags -> Ctrl-V is still the fastest solution I've seen as mentioned in this answer: https://stackoverflow.com/a/5109994/486325.
Upvotes: 54
Reputation: 8273
I know this is old and you have probably found the answer by now but I would just like to add for the sake of those who might not know it that this is possible in VS 2010:
ctrl-k
ctrl-s
(or right-click and select Surround with...
.You can create your own SurroundsWith snippets if you do not find what you are looking for:
File
and then click New
, and choose a file type of XML
.File
menu, click Save
.Save as
box, select All Files (*.*)
.File name
box, enter a file name with the .snippet
file name extension.Save
.Enter something like the following sample in the XML file:
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>ul-div</Title>
<Author>Microsoft Corporation</Author>
<Shortcut>ul>li</Shortcut>
<Description>Wrap in a ul and then an li</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>selected</ID>
<ToolTip>content</ToolTip>
<Default>content</Default>
</Literal>
</Declarations>
<Code Language="html"><![CDATA[<ul><li>$selected$</li></ul>$end$]]></Code>
</Snippet>
</CodeSnippet>
Tools
> Code Snippets Manager
.Import
and browse to the snippet you just created.My HTML Snippets
and click Finish
and then OK
.You will then have your shiny new HTML snippet available for wrapping stuff in!
Upvotes: 68
Reputation: 3043
I know this is an ancient thread but having come up against the issue I finally got round to making my own and as this is one of the first results in Google I figured people might find this useful.
Actually it was pretty easy, I just copied from an existing HTML snippet and moved around the literals. The following snippet will surround with a generic HTML tag, it prompts for the tag and will put it in both the opening and closing tags.
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<!-- Generic HTML Snippet -->
<Header>
<Title>Html</Title>
<Author>Liam Slater</Author>
<Shortcut>h</Shortcut>
<Description>Markup snippet for HTML</Description>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>tag</ID>
<ToolTip>tag</ToolTip>
<Default></Default>
</Literal>
<Literal>
<ID>selected</ID>
<ToolTip>content</ToolTip>
<Default>content</Default>
</Literal>
</Declarations>
<Code Language="html"><![CDATA[<$tag$>$selected$</$tag$>$end$]]></Code>
</Snippet>
</CodeSnippet>
Upvotes: 8
Reputation: 5501
Nothing I'm aware of, but writing a macro to wrap it in whatever tag you want shouldn't be hard. I have a similar one that will wrap my selection in a region block.
Upvotes: 2
Reputation: 532485
When faced with this situation, I often type the closing tag first, then the opening tag. This prevents the IDE from "helping" by inserting the closing tag where I don't want it. I'm also interested in a better solution, though.
Upvotes: 7