Francois Beaussier & Aymeric Gaurat Apelli

Monday

Visual Studio Macro to create a new snippet

Aymeric Gaurat-Apelli: Thanks to François Beaussier for hosting this blog post as my technical blog is no longer available.

Summary

I have written a Visual Studio macro that turns Visual Studio into a Snippet Editor. You simply select the piece of text you want to use in your snippet and the macro will create a snippet that will automatically be loaded by Visual Studio.

Download the latest macro here.

How to use it?

Write the snippet code from within Visual Studio as if you were writing code. Use the syntax $Anything$ to declare Intellisense literals. ($end$ and $selected$ are already used by Visual Studio)


Run the macro from the Macro Explorer:


I suggest you to associate it to a keyboard shortcut, like Alt+S:


The macro will prompt you for the name of the new snippet:


That's it! You can now use your new snippet in Visual Studio.



How to install it?

Open the Macro Explorer by pressing Alt+F8 or through the Tools menu:


In the Macro Explorer, right-click on the Macros node, select "Load Macro Project..." and choose the vsmacros file you downloaded (here)


You should be able to see the "CreateSnippet" macro in the Macro Explorer:


Finally, you will need to edit the macro to fit your environment settings:


Change the author and snippetsPath variables. snippetPaths should be a path that has been added to the Macros Manager (the My Code Snippets folder exists by default)


Save the macro and you are good to go!

Thanks

Big thanks to Patrick Galluci who wrote this article about snippets. My macro is a simple improvement of his great macro.