Avijit Sinha
Avijit Sinha

Reputation: 63

Automatically add a code snippet when a file is created in Visual Studio Code

Is there some way to add a code snippet to a file when it is created based on the file type/extension. I want to add header guards when I created a new C++ header file. Example:

#ifndef DIRECTORY_FILENAME_H
#define DIRECTORY_FILENAME_H

#endif // DIRECTORY_FILENAME_H

I've already created a code snippet that adds this text, but I can't figure out how to add this automatically when a file is created.

Upvotes: 1

Views: 1667

Answers (1)

Alex
Alex

Reputation: 67541

https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.auto-snippet

Example:

autoSnippet.snippets: [
    { "pattern": "ut-*.cpp", "snippet": "ut-template",
    { "pattern": "*.h", "snippet": "header-template" },
    { "pattern": "*.cpp", "snippet": "body-template",
    { "language": "javascript", "snippet": "template", "commands":"[ editor.action.commentLine" ] }
]

Upvotes: 3

Related Questions