Since Matt Ranlett has now harassed me several times for removing the Google/MSDN macro from my blog, I decided to repost it. I have added Live Search to the mix this time and I have tested all of the searches on Visual Studio 2008 Beta 2.
The background on this was that I was spending a lot of time on Google and MSDN (aren’t we all?) and was going back and forth from Visual Studio to the browser, so I decided to try and integrate my searches into Visual Studio. The resultant experience is that you can highlight any text in Visual Studio and press your assigned keyboard shortcut and receive search results from your favorite search engines WITHIN the Visual Studio IDE. It really comes in handy!
In the code below, I have provided the URLs for searching against Google, Live, and MSDN. It should be pretty obvious when you look at the macro what you would need to do to configure any other search engines. Be sure to uncomment one (and only one) of the
url lines in the code when you setup each macro.
For those not familiar with macros, here are the steps to add a macro in Visual Studio:
1) Goto Tools | Macros | Macros IDE
2) Once in the Macros IDE, you can create a new Module or you can just add this to the default “My Macros” module.
3) To add to My Macros, just right click on it in the Project Explorer and choose Add | Add New Item. (if you don’t see the Project Explorer,
try View | Project Explorer).
4) When the Add New Item Dialog shows up, select Code File and enter GoogleSearch and click OK.
5) Paste the Code for GoogleSearch below into this file and save it. Make
sure that you uncomment one of the url lines in the code.
6) Click Debug | Build and then close the Macros IDE.
7) Once back in VS2005, click Tools | Customize and click the Keyboard button at the bottom of the Customize Dialog.
In the Show Commands Containing box, type “macros” and look for an entry that looks like this: “Macros.MyMacros.GoogleSearch.GoogleSearch”. (This could be different depending on where you created the macro).
9) Once you have the correct macro selected, go to the Press Shortcut Keys box and type whatever keys you want to fire the macro (I used Alt-G for Google, Alt-M for MSDN, Alt-L for Live).
10) Click the Assign button and then OK and then finally Close back on the Customize Dialog.
11) Highlight any text in the IDE and fire off your shortcut key to test.
12) Repeat for each search macro you want to setup.
Here is the final code for the macro:
- Imports EnvDTE
- Imports System.Text.RegularExpressions
-
- Public Module GoogleSearch
- Sub GoogleSearch()
- Dim url As String
- Dim selectedText As TextSelection = DTE.ActiveDocument.Selection()
-
- If Not String.IsNullOrEmpty(selectedText.Text) Then
- ' uncomment below for Google
- ' url = String.Format("www.google.com/search?q={0}",
- ' Regex.Replace(selectedText.Text, "\s{1,}", "+"))
-
- ' uncomment below for MSDN search
- ' url = String.Format("http://search.msdn.microsoft.com/search/Default.aspx?brand=msdn&query={0}",
- ' Regex.Replace(selectedText.Text, "\s{1,}", "+"))
-
- ' uncomment below for Live search
- ' url = String.Format("http://search.live.com/results.aspx?q={0}",
- ' Regex.Replace(selectedText.Text, "\s{1,}", "+"))
-
- DTE.ExecuteCommand("View.WebBrowser", url)
- Else
- MsgBox("No text selected.")
- End If
- End Sub
- End Module