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.


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.Text = "{"
    DTE.ActiveDocument.Selection.Text = "}"
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!


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( _

        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="">
    <CodeSnippet Format="1.0.0">
            <Description>Code snippet for { get; set; }</Description>
            <Author>Microsoft Corporation</Author>
            <Code Language="csharp"><![CDATA[{ get; set; }$end$]]>

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