Coding productivity: macros, shortcuts and snippets


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:

Void1

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:

ConvertAutopropToProp1

Try it out!

ConvertFieldToAutoProp

Suppose you have a field which you'd like to convert to an auto-implemented property:

ConvertFieldToAutoprop1

And when you click the menu item, you get:

ConvertFieldToAutoprop2

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.

kick it on DotNetKicks.com

Comments (19)

  1. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  2. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  3. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  4. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  5. Вот здесь можно посмотреть предыдущий пост из серии списка постов. Также некоторые ссылки на мои популярные

  6. 【原文地址】 April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight 【原文发表日期】 Friday

  7. ASP.NET Meer ASP.NET Beveiliging Tutorials : De drie laatste fantastische ASP.NET beveiliging tutorials

  8. Programming says:

    Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  9. Voici l&#8217;article le plus r&#233;cent dans ma s&#233;rie Listes de liens .&#160; Visitez aussi ma

  10. ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش

  11. ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش

  12. homam says:

    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.NewLine()

           DTE.ActiveDocument.Selection.Text = typeName + ".prototype.get_" + propertyName + " = function() { return this." + propertyName + "; }"

           DTE.ActiveDocument.Selection.NewLine()

       End Sub

    </pre>

    Homam

  13. Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the

  14. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  15. Insert Curly Braces Macro for C#

  16. matxkas says:

    what are the steps to coding a macro and assigning a key for example … have the macro

    ask the user to parameters to run ipconfig …

    John

  17. I decided to compile a small list of Visual Studio features that I use a lot. Code Editor Code Snippets

  18. Mike says:

    Short cuts are great for increasing programmer productivity. I use them all the time…

Skip to main content