Visual Studio macros are a fantastic productivity booster, which is often under-estimated. It's so easy to record a macro for your repetitive action and then just play it back. Even better, map a macro to a keyboard shortcut. I'll share a couple of examples.
InsertCurlies
If you open up Visual Studio and type in this code:
How many keystrokes did you need? I've got 10 (including holding the Shift key once). It's because I have this macro mapped to Shift+Enter:
Sub InsertCurlies() DTE.ActiveDocument.Selection.NewLine() DTE.ActiveDocument.Selection.Text = "{" DTE.ActiveDocument.Selection.NewLine() DTE.ActiveDocument.Selection.Text = "}" DTE.ActiveDocument.Selection.LineUp() DTE.ActiveDocument.Selection.NewLine() End Sub
So I just typed in void Foo() and hit Shift+Enter to insert a pair of curlies and place the cursor inside. Remarkably, I've noticed that with this macro I now almost never have to hit the curly brace keys on my keyboard. Readers from Germany will especially appreciate this macro, because on German keyboard layouts you have to press Right-Alt and the curly key, which really takes some time to get used to.
This macro is also useful to convert an auto-property to a usual property: you select the semicolon and hit Shift+Enter:
Try it out!
ConvertFieldToAutoProp
Suppose you have a field which you'd like to convert to an auto-implemented property:
And when you click the menu item, you get:
How did I do it? First, here's the macro:
Sub ConvertFieldToAutoprop() DTE.ActiveDocument.Selection.StartOfLine( _ vsStartOfLineOptions.vsStartOfLineOptionsFirstText) DTE.ActiveDocument.Selection.EndOfLine(True) Dim fieldtext As String = DTE.ActiveDocument.Selection.Text If fieldtext.StartsWith("protected") _ Or fieldtext.StartsWith("internal") _ Or fieldtext.StartsWith("private") Then fieldtext = fieldtext.Replace("protected internal", "public") fieldtext = fieldtext.Replace("protected", "public") fieldtext = fieldtext.Replace("internal", "public") fieldtext = fieldtext.Replace("private", "public") ElseIf Not fieldtext.StartsWith("public") Then fieldtext = "public " + fieldtext End If fieldtext = fieldtext.Replace(";", " { get; set; }") DTE.ActiveDocument.Selection.Text = fieldtext End Sub
And then just add the macro command to the refactor context menu or any other place. This may seem like no big deal, but I had to convert fields to auto-properties recently in 50+ files. I really learned to appreciate this macro.
gs code snippet
This is a very little but useful snippet: gs expands to { get; set; }
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>gs</Title> <Shortcut>gs</Shortcut> <Description>Code snippet for { get; set; }</Description> <Author>Microsoft Corporation</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Code Language="csharp"><![CDATA[{ get; set; }$end$]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Although I usually use the prop snippet to create auto-implemented properties, but gs is useful in some cases as well.
I hope this has inspired you to do some personal usability research experiments and define everyday actions that you can optimize using macros, snippets and shortcuts. I would love to hear about your personal tips and tricks as well.
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Here is the latest in my link-listing series .  Also check out my ASP.NET Tips, Tricks and Tutorials
Вот здесь можно посмотреть предыдущий пост из серии списка постов. Также некоторые ссылки на мои популярные
【原文地址】 April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight 【原文发表日期】 Friday
ASP.NET Meer ASP.NET Beveiliging Tutorials : De drie laatste fantastische ASP.NET beveiliging tutorials
Voici l’article le plus récent dans ma série Listes de liens .  Visitez aussi ma
ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش
I wrote this Macro today, it covers the lack of 'prop' snippet in JavaScript editor:
<pre>
Sub JSProp()
Dim text As String = DTE.ActiveDocument.Selection.Text
Dim vars As String() = text.Split(".")
Dim typeName As String = vars(0)
Dim propertyName As String = vars(1)
DTE.ActiveDocument.Selection.Text = typeName + ".prototype." + propertyName + " = null;"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = typeName + ".prototype.set_" + propertyName + " = function(value) { this." + propertyName + " = value; }"
DTE.ActiveDocument.Selection.Text = typeName + ".prototype.get_" + propertyName + " = function() { return this." + propertyName + "; }"
End Sub
</pre>
Homam
Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the
Insert Curly Braces Macro for C#