Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro to wrap selected text with tags in Visual Studio

I realize that I may be being a bit lazy, but does anyone know of a Visual Studio macro, where I can select some text inside of the Visual Studio IDE, click a button, and have it wrap the selected text with tags? It would generate something like:

<strong>My Selected Text</strong>

I would even be up for creating a macro, just not sure where to exactly start!

like image 784
mattruma Avatar asked Feb 28 '09 14:02

mattruma


4 Answers

The code to do so is rather simple:

Sub SurroundWithStrongTag()
    DTE.ActiveDocument.Selection.Text = "<strong>" + DTE.ActiveDocument.Selection.Text + "</strong>"
End Sub

Now, if you don't know much about macros here's how to add it:

  • First you need open the macros IDE, click Tools->Macros->Macros IDE...
  • Next, we will add a module for your custom macros. Right click on "MyMacros" in the Project Explorer, click Add->Add Module..., type in an appropriate name then click "Add".
  • Now paste the function inside the module, making copies for any other tags you want
  • Save and close the macros IDE

To hook the macro up to a button:

  • Click Tools->Customize...
  • Click New..., type in an appropriate name, click OK. An empty toolbar should be visible (you may have to move the window to see it)
  • Click the Commands tab, and select "Macros" in categories
  • Find the macros created before and drag them over to the toolbar
  • Right click the buttons to change settings (such as displaying an icon instead of text)
like image 144
redwyre Avatar answered Nov 11 '22 16:11

redwyre


I know this is an old topic, but maybe someone finds this useful.

I have the following set up:

Sub WrapInH1()
    WrapInTag("h1")
End Sub

Sub WrapInP()
    WrapInTag("p")
End Sub

Sub WrapInStrong()
    WrapInTag("strong")
End Sub

Sub WrapInTag()
    WrapInTag("")
End Sub

Sub WrapInTag(ByVal tagText As String)
    EnableAutoComplete(False)

    If tagText.Length = 0 Then
        tagText = InputBox("Enter Tag")
    End If

    Dim text As String
    text = DTE.ActiveDocument.Selection.Text
    text = Regex.Replace(text, vbCrLf & "$", "") 'Remove the vbCrLf at the end of the line, for when you select the line by clicking in the margin, otherwise your closing tag ends up on it's own line at the end...

    DTE.ActiveDocument.Selection.Text = "<" & tagText & ">" & text & "</" & tagText & ">" & vbCrLf
    EnableAutoComplete(True)
End Sub

Private Sub EnableAutoComplete(ByVal enabled As Boolean)
    Dim HTMLprops As Properties
    Dim aProp As EnvDTE.Property
    HTMLprops = DTE.Properties("Texteditor", "HTML Specific")
    aProp = HTMLprops.Item("AutoInsertCloseTag")
    aProp.Value = enabled
End Sub
like image 29
Albert Avatar answered Nov 11 '22 15:11

Albert


Dim HTMLprops As Properties = DTE.Properties("Texteditor", "HTML Specific")

Dim aProp As EnvDTE.Property = HTMLprops.Item("AutoInsertCloseTag")

aProp.Value = False
like image 1
neil Avatar answered Nov 11 '22 16:11

neil


Original answer

If you want an out of the box solution, Visual Studio 2015 comes with a new shortcut, Shift+Alt+W 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.

Example

Shift+Alt+W > strong > Enter
like image 1
djones Avatar answered Nov 11 '22 15:11

djones