Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awesome Visual Studio Macros [closed]

I add buttons on the toolbar for the following 3 macros. Each will take the currently selected text in any file and google it (or MSDN-it, or spell-check-it). Make up a nifty icon for the toolbar for extra style-points.

Private Const BROWSER_PATH As String = "C:\Program Files\Mozilla Firefox\firefox.exe"

Sub SearchGoogle()
    Dim cmd As String
    cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
    Shell(cmd, AppWinStyle.NormalFocus)
End Sub

Sub SearchMSDN()
    Dim cmd As String
    cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}+site%3Amsdn.microsoft.com", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
    Shell(cmd, AppWinStyle.NormalFocus)
End Sub

Sub SpellCheck()
    Dim cmd As String
    cmd = String.Format("{0} http://www.spellcheck.net/cgi-bin/spell.exe?action=CHECKWORD&string={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
    Shell(cmd, AppWinStyle.NormalFocus)
End Sub

Show build duration in the Output window

Put this code in your EnvironmentEvents module. This will write the duration directly to the build window for any action on a solution (build, rebuild, clean, deploy).

You can change the IsBuild function to specify the actions you want to see this information for.

Dim buildStart As Date

Private Function IsBuild(ByVal scope As EnvDTE.vsBuildScope, ByVal action As EnvDTE.vsBuildAction) As Boolean
    Return scope = vsBuildScope.vsBuildScopeSolution
End Function

Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
    If (IsBuild(Scope, Action)) Then
        buildStart = Date.Now
    End If
End Sub

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
    If (IsBuild(Scope, Action)) Then
        Dim buildTime = Date.Now - buildStart
        WriteToBuildWindow(String.Format("Build time: {0}", buildTime.ToString))
    End If
End Sub

Private Sub WriteToBuildWindow(ByVal message As String)
    Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim ow As OutputWindow = CType(win.Object, OutputWindow)
    For Each owPane As OutputWindowPane In ow.OutputWindowPanes
        If (owPane.Name.Equals("Build")) Then
            owPane.OutputString(message)
            Exit For
        End If
    Next
End Sub

Show the start page after you close a solution (but keep Visual Studio open)

Put this code in your EnvironmentEvents module:

Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
    DTE.ExecuteCommand("View.StartPage")
End Sub


Hide the start page after you open a solution

Put this code in your EnvironmentEvents module:

Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
    Dim startPageGuid As String = "{387CB18D-6153-4156-9257-9AC3F9207BBE}"
    Dim startPage As EnvDTE.Window = DTE.Windows.Item(startPageGuid)
    If startPage IsNot Nothing Then startPage.Close()
End Sub


These two together will cause your Start Page to hide itself when you open a solution. When you close the solution, the Start Page comes back.


I use the following lesser-known shortcuts very often:

  • Ctrl+Enter: Insert a blank line above the current line (and place the cursor there)
  • Ctrl+Shift+Enter: Insert a blank line below the current line (and place the cursor there)
  • Ctrl+Shift+V: Cycles the clipboard ring