No one likes writing boilerplate code. VS Code Snippets to the rescue! Let’s make snippets to automate new file templates. Go to File -> Preferences -> Configure Snippets and type cpp.json. This will bring up the C++ json snippet file. Add this entry for a boilerplate header template:
{
"Header Template": {
"prefix": "new-header",
"body": [
"#ifndef _${1:HEADER_NAME}_H",
"#define _${1:HEADER_NAME}_H",
"",
"",
"/*Defines*/",
"",
"/*Typedefs, enums and structure definitions.*/",
"",
"/*Private variables.*/",
"",
"/*Public function prototypes.*/",
"",
"/*Private function prototypes.*/",
"",
"#endif // _$1_H"
],
"description": "Template for header files"
}
}
Next, open the c.json snippet file and add this for a boilerplate c file:
{
"C File Template": {
"prefix": "new-c-file",
"body": [
"/*",
" File Description:",
"*/",
"",
"#include \"Common.h\"",
"",
"",
"/*Local Defines*/",
"",
"/*Typedefs, enums and structures*/",
"",
"/*Static variables*/",
"",
"/*Static function prototypes*/",
"",
"/*Functions*/",
"",
],
"description": "Template for header files"
},
}
Save these json files and hit ctrl + shift + P and type “Reload Window”, then enter. This will ensure the new snippets are loaded.
Make a new header file. In the file, type “new-header” and the snippet will appear in the auto-complete dropdown. Select it and hit tab to insert the boilerplate. It even highlights the include guard. Type the name of the include file there, then hit tab and the text is changed in the three required places. Similarly, make a new c file. In the file, type new-c-file and select/hit tab. There’s your c file boilerplate.
Don't write C? No problem. You probably noticed thd other file types in the Configure Snippets drop-down. Choose the file type of interest and put your snippet there.