Alternative to macros in Visual Studio 2012
Solution 1:
The simplest alternative to macros is creating add-ins. I know, I know, I wasn't excited about it either, but it's actually surprisingly easy. There are three simple parts to it:
- Create the macro project, stepping through a wizard UI.
- Write your code.
- Copy the macro's .addin and .dll files to your Visual Studio
Addins
directory.
Let's take a simple macro I wrote to show the Start Page after closing a solution and turn it into an add-in.
Create the macro project
- Run VS 2012 and create a new project.
- Go to Templates > Other Project Types > Extensibility and select Visual Studio Add-in.
- Give it a name, such as ShowStartPage.
- Click OK. This brings up the Add-in Wizard.
- Step through the wizard, choosing:
- Programming language: we'll use C#
- Application host: VS 2012 should be selected
- Name and description for your add-in
- On the add-in options page, checkmark only the second option ("I would like my Add-in to load when the host application starts")
- Skip past the About Box stuff for now, and click Finish.
Now you have an add-in project. Here's what you do with it:
Write the code
Open the Connect.cs
file. (It might already be open. Some of the "DTE" stuff should look familiar.)
Add this code at class level:
SolutionEvents solutionEvents;
Add this code to the OnConnection
method, right after the _addInInstance = (AddIn)addInInst;
line:
solutionEvents = _applicationObject.Events.SolutionEvents;
solutionEvents.AfterClosing += () =>
{
_applicationObject.ExecuteCommand("View.StartPage");
};
Hit the "Run" button to test your code. A new instance of Visual Studio 2012 starts up, with your add-in loaded. Now test the add-in and make sure it works. (Open a solution, then close it; the Start Page should return when you do.)
Deploy it
Once the add-in works, to use it regularly with Visual Studio 2012, you only need to deploy two files:
-
ShowStartPage.AddIn
(from your main project directory) -
ShowStartPage.dll
(from your project's build directory; e.g. bin\Debug or bin\Release)
Put those two files in your VS 2012 add-ins directory, probably located here:
C:\Users\[your user name]\Documents\Visual Studio 2012\Addins
Then exit and restart Visual Studio, and you should see your add-in working. You should also see it listed when you go to Tools > Add-in Manager.
While this is a bit more of a nuisance than just opening the macro editor and sticking your macro code in there, it does have the advantage that you can use any language you want, instead of being stuck with the somewhat flaky VB-like editor in past versions of Visual Studio.
Solution 2:
The Visual Commander extension (developed by me) is an alternative to macros in Visual Studio 2012/2013/2015. You can even reuse your existing Visual Studio macros code in new VB commands.
Solution 3:
I'll stick to cutting the text into Notepad++ and using macros there, then pasting back. It is a shame the feature isn't in Visual Studio 2012 any more...
Solution 4:
There is an add-in, VSScript, for Visual Studio which replaces missing macros functionality. Although it does not use Visual Basic, but the Lua scripting language, you might want to try it out.
There is a recorder, macro code editor window with IntelliSense, and a simple debugger. The add-in also supports earlier versions of Visual Studio, so if you prefer the Lua language rather than Visual Basic, you can use it instead original Visual Studio macros.
Solution 5:
I was very sad to see Macros go too. You can get close with substitutions using the regular expression search and replace inside of Visual Studio 2012. In your case:
Find:
(.*) (.*);
Replace with:
private $1 _$2;\npublic $1 $2\n{\n get {return _$2;}\n set\n {\n if(_$2 = value;\n RaisePropertyChanged("$2");\n }\n}\n
That will get you everything except capitalization of property names which Macros would be better for.
But one advantage of the regular expression approach is when the input isn't as simple (e.g. database table DDL statements).
Here are a couple of useful links from MSDN:
Substitutions in Regular Expressions
Using Regular Expressions in Visual Studio