views:

261

answers:

4

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.

A: 

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.

tvanfosson
+1  A: 

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.

Ian Jacobs
A: 

I'm still looking for an answer to this. I'm using VS2010 now. Could there be a shortcut in it?

john west
See my answer posted above.
Bradley Mountford
+1  A: 

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:

  1. Select the code you would like to surround.
  2. Do ctrl-k ctrl-s (or right-click and select "Surround with...".
  3. There are a variety of HTML snippets to choose from.

You can create your own SurroundsWith snippets if you do not find what you are looking for:

  1. Click XML File and then click Open.
  2. On the File menu, click Save .
  3. In the Save as type box, select All Files (.).
  4. In the File name box, enter a file name with the .snippet file name extension.
  5. Click Save.

Enter something like the following sample in the XML file:

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  <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>
  1. Open Tools > Code Snippets Manager.
  2. Click Import and browse to the snippet you just created.
  3. Check "My HTML Snippets" and click Finish and then OK.

You will then have your shiny new HTML snippet available for wrapping stuff in!

Bradley Mountford