Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(){

}
like image 812
furfur Avatar asked Jun 15 '19 08:06

furfur


2 Answers

  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!

like image 61
Yash Verma Avatar answered Sep 17 '22 17:09

Yash Verma


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

like image 38
Mayukh Pankaj Avatar answered Sep 20 '22 17:09

Mayukh Pankaj