2

How can I setup Visual Studio 2013 to populate new C++ header and cpp files with default text?

I know I could create a custom snippet and then press the keys to insert it but it would be nice to have this automatically inserted when the new file is created.

Eg; insert the following into new .h files:

#ifndef PROJECT_NAME_FILE_NAME_H
#define PROJECT_NAME_FILE_NAME_H

class FileName
{
public:
    // Static Variables //

    // Static Methods //

    // Class Variables //

    // Class Methods //

protected:
    // Static Variables //

    // Static Methods //

    // Class Variables //

    // Class Methods //

private:
    // Static Variables //

    // Static Methods //

    // Class Variables //

    // Class Methods //

};

#endif // PROJECT_NAME_FILE_NAME_H
sazr
  • 24,984
  • 66
  • 194
  • 362
  • I think this might help you: http://stackoverflow.com/questions/2072687/how-do-i-edit-the-visual-studio-templates-for-new-c-sharp-class-interface – Gregor Menih Jan 24 '16 at 00:17

1 Answers1

2

You may update the default models provided by MSCV.

I have no longer MSVC2013 installed but for MSVC2015 they are in directory C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcprojectitems : hfile.h for the headers, and newc++file.cpp for cpp files.

Attention: to edit these files, you need to run your editor with administrator priviledge.

Another approach could be to create your own item templates. This is pretty easy: you start a project, add a .cpp or a .h, change it with the content you want to be used. Then export it (File->export template...). Here there is a step by step approach. You must make sure that at the end, the generated .zip file is copied in your user's directory to the folder Visual Studio 2013\Templates\ItemTemplates in the existing C++ subfolder.

Important notice: new templates might be taken into account at startup of MSVC only. SO be sure you exit and enter again MSVC to see the new templates added.

Edit following additional question in comments

In your templates you can use template parameters, that will be automatically replaced when the item is added to a project (provided ReplaceParameters="true" in the .vstemplate file of the exported template).

Example: a header template designed to ensure an include guard:

// $itemname$.h
// by $username$

#ifndef $safeitemname$_H
#define $safeitemname$_H

// TO DO: your guarded header here

#endif

When adding a new element using this header template, the paramter substitution will result in:

// my_funny_header.h
// by Christophe

#ifndef my_funny_header_H
#define my_funny_header_H

// TO DO: your guarded header here

#endif

Note however, that not all the paramters can be used. For example $projectname$ works only for project templates, not for itemtemplates. Note also that $safeitemname$ should be preferred to $itemname$ in symbol definition.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    thanks. The path on VS2013 is `C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcprojectitems`. Have you been able to use VS macros in these files? For eg `#ifndef $(ProjectName)_$(TargetName)_H`. Currently these are not being converted when the header file is created. – sazr Jan 24 '16 at 09:43
  • @JakeM Fine ! Yes you can use template parameters, but with some limitations. See my edit. – Christophe Jan 24 '16 at 18:07