3

How to write a snippet for 'main' function in vs code? It's not allowing to put double "" in between the body. Here is what I have written:

"Print to console": {
        "prefix": "mai",
        "body": [
            "if __name__ == __main__:"
            
        ],
        "description": "This throws main"

And I want this:

if __name__ == '__main__':
   pass

How to make this snippet?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Edeath
  • 31
  • 5

2 Answers2

3

Try this one.

"Print to console": {
        "prefix": "mai",
        "body": [
            "if __name__ == '__main__':",
            "\tpass"
        ],
        "description": "Whatever you want to write."
    }

codester_09
  • 5,622
  • 2
  • 5
  • 27
1

Paste into https://snippet-generator.app/ that formats it into the necessary json. You copy that (there’s a button) and then you just paste it into your snippet file.

For who need to know I'd guess you just have to do the right escape "foo" : "\"bar\"" (I think). Then again, Python is OK with single quotes and json doesn't care about about those so "foo" : "'bar'" would also bear looking into. Snippet generator does not round trip, so I do typically have to edit existing snippets with the correct escape/quotes strategy, but it's easier to get 95% of the thing right and then do tweaks than write embedded Python in json all the time.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73