VSNewFile: A Visual Studio Addin to More Easily Add New Items to a Project

My first Visual Studio Add-in! Creating add-ins is pretty simple, once you get used to the CommandBar model it is using, which is apparently a general Office suite extensibility mechanism.

Anyway, let me first explain my motivation for this. It started out as an academic exercise, as I have always wanted to dip my feet in a little VS extensibility. But I thought of a legitimate need for an add-in, at least in my personal experience, so it took on new life. But I figured I can’t be the only one who has felt this way, so I decided to publish the add-in, and host it on GitHub (VSNewFile on GitHub) hoping to spur contributions.

Adding Files the Built-in Way

Here’s the problem I wanted to solve. You’re working on a project, and it’s time to add a new file to the project. Whatever it is – a class, script, html page, aspx page, or what-have-you, you go through a menu or keyboard shortcut to get to the “Add New Item” dialog. Typically, you do it by right-clicking the location where you want the file (the project or a folder of it):

AddNewDialog

This brings up a dialog that contains, well, every conceivable type of item you might want to add. It’s all the available item templates, which can result in anywhere from a ton to a veritable sea of choices. To be fair, this dialog has been revamped in Visual Studio 2010, which organizes it a little better than Visual Studio 2008, and adds a search box. It also loads noticeably faster.

image 
To me, this dialog is just getting in my way. If I want to add a JavaScript script to my project, I don’t want to have to hunt for the script template item in this dialog. Yes, it is categorized, and yes, it now has a search box. But still, all this UI to swim through when all I need is a new file in the project. I will name it. I will provide the content, I don’t even need a ‘template’.

VS kind of realizes this. In the add menu in a class library project, for example, there is a “Add Class…” choice. But all this really does is select that project item from the dialog by default. You still must wait for the dialog, see it, and type in a name for the file. How is that really any different than hitting F2 on an existing item? It isn’t.

Adding Files the Hack Way

What I often find myself doing, just to avoid going through this dialog, is to copy and paste an existing file, rename it, then “CTRL-A, DEL” the content. In a few short keystrokes I’ve got my new file. Even if the original file wasn’t the right type, it doesn’t matter – I will rename it anyway, including the extension. It works well enough if the place I am adding the file to doesn’t have much in it already. But if there are a lot of files at that level, it sucks, because the new file will have the name “Copy of xyz”, causing it to be moved into the ‘C’ section of the alphabetically sorted items, which might be far, far away from the original file (and so I tend to try and copy a file that starts with ‘C’ *evil grin*).

Using ‘Export Template’

To be completely fair I should at least mention this feature. I’m not even sure if this is new in VS 2010 or not (I think so). But it allows you to export a project item or items, including potential project references required by it. Then it becomes a new item in the available ‘installed templates’. No doubt this is useful to help bootstrap new projects. But that still requires you to go through the ‘New Item’ dialog.

Adding Files with VSNewFile

So hopefully I have sufficiently defined the problem and got a few of you to think, “Yeah, me too!”… What VSNewFile does is let you skip the dialog entirely by adding project items directly to the context menu. But it does a bit more than that, so do read on.

For example, to add a new class, you can right-click the location and pick that option. A new .cs file is instantly added to the project, and the new item is selected and put into the ‘rename’ mode immediately.

VSNewFileMenuVSNewFileMenu_result


The default items available are shown here. But you can customize them. You can also customize the content of each template. To do so, you create a directory in your documents folder, ‘VSNewFile Templates’. In there, you drop the templates you want to use, but you name them in a particular way.

For example, here’s a template that will add a new item named “Add TITLE”. It will add a project item named “SOMEFILE.foo” (or ‘SOMEFILE1.foo’ if that exists, etc).

image

The format of the file name is:

<ORDER>_<KEY>_<BASE FILENAME>_<ICON ID>_<TITLE>.<EXTENTION>

Where:

<ORDER> is a number that lets you determine the order of the items in the menu (relative to each other).
<KEY> is a case sensitive identifier different for each template item. More on that later.
<BASE FILENAME> is the default name of the file, which doesn’t matter that much, since they will be renaming it anyway.
<ICON ID> is a number the dictates the icon used for the menu item. There are a huge number of built-in choices. More on that later.
<TITLE> is the string that will appear in the menu.

And, the contents of the file are the default content for the item (the ‘template’). The content of the file can contain anything you want, of course. But it also supports two tokens: %NAMESPACE% and %FILENAME%, which will be replaced with the corresponding values.

Here is the content of this sample:

testing
Namespace = %NAMESPACE%
Filename = %FILENAME%

I kind went back and forth on this. I could have made it so there’d be an XML or JSON file that defines the templates, instead of cramming all this data into the filename itself. I like the simplicity of this better. It makes it easy to customize since you can literally just throw these files around, copy them from someone else, etc, without worrying about merge data into a central description file, in whatever format. Here’s our new item showing up:

samplesomefile

Practical Use

One immediate thing I am using this for is to make it easier to add very commonly used scripts to my web projects. For example, uh, say, jQuery? :) All I need to do is drop jQuery-1.4.2.js and jQuery-1.4.2.min.js into the templates folder, provide the order, title, etc, and then instantly, I can now add jQuery to any project I have without even thinking about “where is jQuery? Can I copy it from that other project?”

image image 

Using the KEY

There are two reasons for the ‘key’ portion of the item. First, it allows you to turn off the built-in, default templates, which are:

  • FILE = Add File (generic, empty file)
  • VB = Add VB Class
  • CS = Add C# Class (includes some basic usings)
  • HTML = Add HTML page (includes basic structure, doctype, etc)
  • JS = Add Script (includes an immediately-invoking function closure)

To turn one off, just include a file with the name “_<KEY>”. For example, to turn off all the items except our custom one, you do this:

image

image

The other reason for the key is that there are new Visual Studio Commands created for each one. This makes it possible to bind a keyboard shortcut to one of them. So you could, for example, have a keyboard combination that adds a new web page to your website, or a new CS class to your class library, etc. Here is our sample item showing up in the keyboard bindings option.

imageEven though the contents of the template directory may change from one launch of Visual Studio to the next, the bindings will remain attached to any item with a particular key, thanks to it taking care not to lose keyboard bindings even though the commands are completely recreated each time.

The Icon Face ID

Visual Studio uses a Microsoft Office style add-in mechanism, I gather. There are a predetermined set of built-in icons available. You can use your own icons when developing add-ins, of course, but I’m no designer. I just wanted to find appropriate-ish icons for the built-in templates, and allow you to choose from an existing built-in icon for your own. Unfortunately, there isn’t a lot out there on the interwebs that helps you figure out what the built-in types are. There’s an MSDN article that describes at length a way to create a program that lists all the icons. But I don’t want to write a program to figure them out! Just show them to me! Sheesh :)

Thankfully, someone out there felt the same way, and uses a novel hack to get the icons to show up in an outlook toolbar. He then painstakingly took screenshots of them, one group at a time. It isn’t complete though – there are tens of thousands of icons. But it’s good enough. If anyone has an exhaustive list, please let me, and the rest of the add-in community know.

Icon Face ID Reference

Installing the Add-in

It will work with Visual Studio 2008 and Visual Studio 2010. Just unzip the release into your Documents\Visual Studio 20xx\Addins folder. It contains the binary and the Visual Studio “.addin” file. For example, the path to mine is:

C:\Users\InfinitiesLoop\Documents\Visual Studio 2010\Addins

Conclusion

So that’s it! I hope you find it as useful as I have. It’s on GitHub, so if you’re into this kind of thing, please do fork it and improve it!

Reference:

13 Comments

  • This is awesome. I'm going to install it shortly. One idea for an extension that I've been wanting to build is similar to this, but only list recently added file types: cs/js/aspx/etc.

  • Awesome plugin! I think I'll get a lot of usage out of this. One question though -- how would I create one for some of the standard VS templates.. i.e., a shortcut for "Form" or "ASP.NET WebForm", etc. that would call on the default VS templates ? -- or is that not possible ?

    Thanks,

    - Matthew

  • Thanks Steve, Matthew :)

    @Matthew: You can't use the default templates, that'd be a cool feature though, I'll think about it :) In the meantime, just copy/paste the content you want into the custom template. The content of the file will be the content of new items using that template.

  • @InfinitiesLoop Re: "In the meantime, just copy/paste the content you want into the custom template. The content of the file will be the content of new items using that template."

    It is not possible, as aspx has to have dependent file created too (for me it is cs).

    Nevertheless it is very useful for all other file types (and I hope that you will add aspx in the future :D )

    Thank you!

    Mialn

  • Great idea! I tried installing it, but I keep receiving an error saying VSNewFile failed to load or caused an exception. The error number is 80131515. I created the template directory and placed a basic file in there to test: 0_CS_CSharpClass_629_C# Class.cs

  • Where does the Addins folder live on an XP box? There's no Addins folder under 'My Documents\Visual Studio 2010'.

  • @Bryan: Just create the Addins folder, it goes there. It won't exist unless you have at least one Addin already.

    @Gordon: VSIX file: Sure, that'd be nice. I thought I saw somewhere that vsix didn't work with add-ins though. Dunno, I'm a newb :)

    @Milan: I realize aspx/aspx.cs is a problem... there's an addin out there that lets you manually cause a file to be a child of another. You could use that in conjunction with this one to workaround it in the meantime.

  • @Miguel: Odd. Does it work without that custom template? You don't have to create the directory or have one for it to work. Are you using VS2008 or VS2010?

  • @InfinitiesLoop I'm using VS2010 and it has the same behavior even without any templates in the VSNewFile Templates folder. I will try downloading it again tomorrow, just in case something got corrupted. Thanks for responding!

  • @Miguel: Do you have .NET 3.5, or only .NET 4.0?

  • Installed the addin in VS 2010 Ultimate. Works great, but the new file does not come up "selected and put in 'rename' mode".

    Also, although the new VB file name is "class.vb" the template shows "Class1.vb". It shouldn't have added the 1 (and it really should have followed the case of the file name).

  • I also experience the same exception as Miguel is getting

  • Great addin!
    A few comments:
    - Namespace does not contain foldernames. (just the base namespace)
    - When I rename the file it should update the Class name in the new file, but it doesnt..
    I use VS2010 ultimate on Win8 dev preview. :)

Comments have been disabled for this content.