2010/01/11

Best Macro to use in Visual Studio

I wrote this little macro awhile back, and after getting burned by yet another accidental hit of the F1 key and waiting the agonizing amount of time before the Help came up, I realized it's time to get this macro out there so I can "install" it wherever I'm at. Since whenever I’m looking up documentation on a .NET class I copy the class name, go to Google, and generally the answer is within the first couple links. So why not automate that.

To add it to your Visual Studio macros, bring up the Macro Explorer panel (View –> Other Windows –> Macro Explorer; or Alt + F8). Edit a module and add sub to the module you want to.



Public Sub SearchWord()
Dim objDocument As EnvDTE.Document = DTE.ActiveDocument
Dim sSearchText As String

Dim currentSelection As TextSelection = objDocument.Selection

If currentSelection.Text <> "" Then
sSearchText = currentSelection.Text
Else
Dim objTextDocument As EnvDTE.TextDocument
Dim objTextSelection As EnvDTE.TextSelection
Dim lineNumber As Integer
Dim colNumber As Integer

' Get the text document
objTextDocument = CType(objDocument.Object, EnvDTE.TextDocument)
objTextSelection = objTextDocument.Selection
colNumber = objTextSelection.ActivePoint.DisplayColumn
lineNumber = objTextSelection.ActivePoint.Line

objTextSelection.WordLeft(False, 1)
objTextSelection.WordRight(True, 1)

sSearchText = objTextSelection.Text
objTextSelection.MoveToDisplayColumn(lineNumber, colNumber)
End If

System.Diagnostics.Process.Start("http://www.google.com/search?q=" + sSearchText)
End Sub


Save the module and you can close out of the Macro editor. Now back in Visual Studio, open up the keyboard options (Tools –> Options –> Environment –> Keyboard). Remove the existing F1 binding by searching for “Help.F1Help” in the “Show commands containing” textbox and clicking the Remove button to unbind it. Now search for the macro you just created. Press the F1 key in the “Press shortcut keys” textbox and click “Assign”.



Now whenever you hit F1 it will highlight the current word and search for it in Google in your default browser. Pretty simple stuff!


EDIT (2010-01-18): Based on feedback I updated the macro to not change the selected text if there's already something selected, and won't leave an item selected if you didn't have something selected already.

11 comments:

Dave Simpson said...

Great code, it would have been great to have just stopped the horrid VS help coming up but to search for the word on Google is just fab!

Aaron Hoffman said...

Simply Amazing. Microsoft - Can this be the default functionality? (or just link directly to the MSDN site instead)

One question though - how do I get this to use Bing instead? ...jk

Eric said...

Very cool. I assigned it to Alt+F1 rather than replacing F1, but before this post I didn't even know you could reassign keys like that at all. Thanks!

Sam J. West said...

I never have used the F1 help key feature. Maybe because in every other product it would never bring me to any context related information. But I GOOGLE stuff constantly!

Thanks for sharing, great macro!

JJoos said...

I removed line 21 and 22, so it doesn't alter my selection. Thanks for posting!

Unknown said...

@JJoos: I suppose that works too. I only have it change the selection so you can put the cursor on the word that you're looking for and it will highlight and search it for you, similar to the default VS help. I'll look into changing it to account for a selection already existing.

Dave said...

Haven't tested this, but in theory you could add "+site:msdn.microsoft.com" to the URL to narrow down the results.

Unknown said...

WOW - that is quick - great piece of code

woosoek said...

how about this one?

http://www.codeproject.com/KB/macros/googlemacro.aspx

Unknown said...

@woosoek: Not too bad of an idea, but I hate having the web browser open up within Visual Studio. Thus the call to open the link directly.

@Dave: That's a good idea too, but many times I've found I'm doing these types of searches on 3rd party libraries as well. That's why I'm not limiting it to just MSDN.

Ashish Sarode said...

Hi Steve,

Thanks for sharing