Got sweet code? Save it!

By | May 9, 2014

If you’re a Visual Studio user you’re more than familiar with snippets (I would hope). ctor, propa, testm, all very useful. But have you created your own snippets?

You can do this with tools like the Snippet Designer extension, or roll your own by creating & editing a snippet XML file by hand based on the snippet schema.

Ain’t nobody got time for that.

Far and away the quickest way I’ve found to save code I reuse often is even simpler than all that. And I’m about to clue you in on it.

If you’ve been to BUILD or other conferences/presentations by Microsoft folk, you’ve undoubtedly seen them take their “sample code” and insert it via a snippet. But what I saw at BUILD 2014 this past April surprised me. All the presenters had the toolbox open *in code view* and were simply dragging & dropping code snippets from the toolbox right in to their code window. Every time they did it, they did so fast that I wasn’t even able to see exactly where things were coming from or what they looked like.

This past week I was preparing for a presentation and decide to just give something a whirl:

image

I got my code all ready in my sample app, and highlighted it. Then I got an idea… what if…

image

Sure enough, just dragging the code over to the toolbox gives you a little ‘text’ entry in your toolbox. You can even rename it so you know what you’re going to use it for:

image

Now, when you want to use it again, you just drag that toolbox item back in to your code:

image

And boom, it appears.

 

Insanely useful for doing live-code presentations, or storing away snippets you use over and over without the hassle of actually writing a snippet.

The best part? These items persist across VS launches; they’ll be there the next time you launch Visual Studio