views:

1510

answers:

5

What are some macros that you have found useful in Visual Studio for code manipulation and automation?

A: 

You might want to add in code snippets as well, they help to speed up the development time and increase productivity.

The standard VB code snippets come with the default installation. The C# code snippets must be downloaded and added seperately. (Link below for those)

As far as macros go, I generally have not used any but the working with Visual studio 2005 book has some pretty good ones in there.

C# Code snippets Link: http://www.codinghorror.com/blog/files/ms-csharp-snippets.7z.zip (Jeff Atwood provided the link) HIH

RZachary
@RZachary - I think that code snippets are independent enough of macros that I went ahead and created a new question for them. It can be found here.
Rob
+5  A: 

This is one of the handy ones I use on HTML and XML files:

''''replaceunicodechars.vb
Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics

Public Module ReplaceUnicodeChars

    Sub ReplaceUnicodeChars()
        DTE.ExecuteCommand("Edit.Find")
        ReplaceAllChar(ChrW(8230), "…")   ' ellipses
        ReplaceAllChar(ChrW(8220), "“")   ' left double quote
        ReplaceAllChar(ChrW(8221), "”")   ' right double quote
        ReplaceAllChar(ChrW(8216), "‘")   ' left single quote
        ReplaceAllChar(ChrW(8217), "’")   ' right single quote
        ReplaceAllChar(ChrW(8211), "–")   ' en dash
        ReplaceAllChar(ChrW(8212), "—")   ' em dash
        ReplaceAllChar(ChrW(176), "°") ' °
        ReplaceAllChar(ChrW(188), "¼") ' ¼
        ReplaceAllChar(ChrW(189), "½") ' ½
        ReplaceAllChar(ChrW(169), "©") ' ©
        ReplaceAllChar(ChrW(174), "®") ' ®
        ReplaceAllChar(ChrW(8224), "†")   ' dagger
        ReplaceAllChar(ChrW(8225), "‡")   ' double-dagger
        ReplaceAllChar(ChrW(185), "¹") ' ¹
        ReplaceAllChar(ChrW(178), "²") ' ²
        ReplaceAllChar(ChrW(179), "³") ' ³
        ReplaceAllChar(ChrW(153), "™")   ' ™
        ''ReplaceAllChar(ChrW(0), "�")

        DTE.Windows.Item(Constants.vsWindowKindFindReplace).Close()
    End Sub

    Sub ReplaceAllChar(ByVal findWhat, ByVal replaceWith)
        DTE.Find.FindWhat = findWhat
        DTE.Find.ReplaceWith = replaceWith
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()
    End Sub

End Module

It's useful when you have to do any kind of data entry and want to escape everything at once.

travis
+1  A: 

This is one I created which allows you to easily change the Target Framework Version of all projects in a solution: http://geekswithblogs.net/sdorman/archive/2008/07/18/visual-studio-2008-and-targetframeworkversion.aspx

Scott Dorman
+1  A: 

I'm using Jean-Paul Boodhoo's BDD macro. It replaces whitespace characters with underscores within the header line of a method signature. This way I can type the names of a test case, for example, as a normal sentence, hit a keyboard shortcut and I have valid method signature.

FantaMango77
+4  A: 

This is my macro to close the solution, delete the intellisense file, and reopen the solution. Essential if you're working in native C++.

Sub UpdateIntellisense()
    Dim solution As Solution = DTE.Solution
    Dim filename As String = solution.FullName
    Dim ncbFile As System.Text.StringBuilder = New System.Text.StringBuilder
    ncbFile.Append(System.IO.Path.GetDirectoryName(filename) + "\")
    ncbFile.Append(System.IO.Path.GetFileNameWithoutExtension(filename))
    ncbFile.Append(".ncb")
    solution.Close(True)
    System.IO.File.Delete(ncbFile.ToString())
    solution.Open(filename)
End Sub
John Richardson
ncbString is never used
xan
@xan: Removed the ncbString for JR
Jon Cage