'About adding a snippet for python in vs code
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?
Solution 1:[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.
Solution 2:[2]
Try this one.
"Print to console": {
"prefix": "mai",
"body": [
"if __name__ == '__main__':",
"\tpass"
],
"description": "Whatever you want to write."
}
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 |