Iris Classon
Iris Classon - In Love with Code

Step by step Macros for Visual Studio

I was asked for a simple example showing how to use/get started with the Macros for Visual Studio extension I mentioned a efw days ago on Twitter. Here is a simple ‘get-started’ I threw together today. I’ll expand this post later with a better example, as well as a video.

The extension can be download here

Some of the features:

  • Record and playback active document operations and Visual Studio IDE commands

  • Playback multiple times

  • Manage and persist macros with a Macro Explorer

  • Assign keyboard bindings to any macro

  • Macro editing in Visual Studio with DTE IntelliSense

  • Sample macros

Once installed the extension can be foun in the Tools menu, and there is also a window you can choose to add to your workspace (Macro explorer).

A macro I made for fun is one that converts camel case properties to pascal case. This is very handy if you use the feature I discussed in the previous post and don’t have Resharper (the feature I’m talking about is the paste JSON as classes feature in Visual Studio). I’m sure somebody is going to tell me that this is already integrated in Visual Studio somehow :D

Step by step

On the property public int last_edit_date { get; set; } I leave the cursor at the last bracket and start recording a macro (Ctrl + M, R)

public int last_edit_date { get; set; }

I use Ctrl + [ to move the cursor to the first bracket, then Ctrl + Left to get to the beginning of the property name.

public int last_edit_date { get; set;}

I hold down Shift and move right on column, highlighting the first letter of the property name.

public int last_edit_date { get; set;}

I enter Ctrl + Shift, U which upper cases that letter.

public int Last_edit_date { get; set;}

Then, to let the macro continue on to the next property I move the cursor down with Down arrow, and then Ctrl + End to get to the first bracket.

I then end recording with Ctrl + M, R(same as start recording). If you save the macro it would look like this:

dte.js =>

  dte.ExecuteCommand("Edit.GotoBrace");  

  dte.ActiveDocument.Selection.WordLeft();  

  dte.ActiveDocument.Selection.CharRight(true);  

  dte.ActiveDocument.Selection.ChangeCase(2);  

  dte.ActiveDocument.Selection.LineDown();  

  dte.ActiveDocument.Selection.EndOfLine();  

The default path for the macros file is:

C:\Users{USER}\AppData\Local\Microsoft\VisualStudio\14.0\Macros\dte.js

Comments

Leave a comment below, or by email.
Adam Griffiths
1/3/2017 9:43:19 AM
Nice - I've never really played around with macros in VS, I had no idea it was so straightforward! 


Last modified on 2016-12-06

comments powered by Disqus