'How can I set up a default code for C++ in visual studio code?

In CodeBlocks it is possible to set up a code that will open each time you create a new file. Kind of a code template or skeleton.

How can I set up a default code to open in Visual Studio Code C++ so I don't have to write it each time I create a new file? I mean, instead of creating a blank file, I need it to show the following code:

#include <iostream>

using namespace std;

int main(){

}


Solution 1:[1]

  1. Go to this link.

  2. Type/Paste the default code you want to use the snippet for. Enter the trigger and description.

  3. Copy the generated snippet onto clipboard.

  4. Head to VSC. Press ctr + shift + p .Type configure user snippets. 5.Select your desired language(C++ in your case).

  5. Replace the comments with the snippet in your clipboard.

  6. Save and exit. Now try to type the trigger text. You will see the snippet ready!

Solution 2:[2]

you can setup user snippet.

1) click on setting icon select user snippets.

enter image description here

2) select your language,cpp.

enter image description here

now enter snippet code,

(snippet code is in JSON)

// Write this code in the cpp.json file
{
    "cpp snippets":
    {
        "prefix" : "basic",
                   "body" : [
                       "#include<iostream>",
                       "using namespace std;",
                       "int main()",
                       "{",
                       "    return 0;",
                       "}"
  
                   ],
                            "description" : "c++ basic"
    }
}

Save this file, now open new .cpp file and just enter "basic" your snippet will appear !

to create custom snippet code, check this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mayukh Pankaj