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.
Comments
Anonymous
April 03, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
April 10, 2008
The comment has been removedAnonymous
April 10, 2008
The comment has been removedAnonymous
April 10, 2008
The comment has been removedAnonymous
April 11, 2008
Вот здесь можно посмотреть предыдущий пост из серии списка постов. Также некоторые ссылки на мои популярныеAnonymous
April 11, 2008
【原文地址】 April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight 【原文发表日期】 FridayAnonymous
April 11, 2008
ASP.NET Meer ASP.NET Beveiliging Tutorials : De drie laatste fantastische ASP.NET beveiliging tutorialsAnonymous
April 14, 2008
The comment has been removedAnonymous
April 15, 2008
Voici l’article le plus récent dans ma série Listes de liens .  Visitez aussi maAnonymous
April 17, 2008
ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزشAnonymous
April 17, 2008
ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزشAnonymous
April 19, 2008
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> HomamAnonymous
April 23, 2008
Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by theAnonymous
June 15, 2008
The comment has been removedAnonymous
July 28, 2008
Insert Curly Braces Macro for C#Anonymous
August 04, 2008
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 ... JohnAnonymous
August 04, 2008
http://blogs.msdn.com/kirillosenkov/archive/2008/05/02/how-to-map-a-visual-studio-macro-to-a-keyboard-shortcut.aspxAnonymous
October 21, 2008
I decided to compile a small list of Visual Studio features that I use a lot. Code Editor Code SnippetsAnonymous
January 25, 2010
Short cuts are great for increasing programmer productivity. I use them all the time...