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:
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="https://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.