1

This is an offshoot of the question found here.

I want to create a new (python) file, and everytime I do so, I want vscode to write to the file the def main if name main idiom:

def main():
  pass

if __name__ == "__main__":
  pass

Is there a json file I can write to? More broadly, can I do this for any file I create like having

#include <iostream>

int main()
{

}

for c++ be written for me when I create a new c++ file?

2
  • 2
    An answer on another thread suggests the extension called Auto Snippet. Commented Dec 14, 2022 at 18:00
  • you can use extension File Templates or use VSC Snippets that gets inserted in file creation Commented Dec 14, 2022 at 20:06

1 Answer 1

1

You can read docs about snippets.

  1. Open File-> Preference -> Configure user snippets
  2. Type python and choose the first one.

enter image description here

  1. Add the following codes to your python.json:
"Print to console": {
    "prefix": "main",
    "body": [
        "def main():",
        "    pass",
        "if __name__ == \"__main__\":",
        "    pass"
    ],
    "description": "Log output to console"
}

Then when you type main you can get the intellisense.

enter image description here.

Another way is to use extension, you can create a template file for example:

main.py

def main():
    pass
if __name__ == "__main__":
    pass

Then use shortcuts "Ctrl+Shift+P" and type "Files: New Template From File".

When you want to use this template, you can use "File: New File From Template" and choose this main.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.