Search code examples
pythonjsonvisual-studio-codevscode-snippets

How to escape python block comments when creating vscode snippets?


I created a vscode snippet for a a python function definiton and I am wondering if this is the only and/or recommended way of escaping the python block comments (""" """).

global snippets file

{   
    "def ": {
            "scope": "python",
            "prefix": "def func",
            "body": [
                "def $1 ():
                \"\"\"\" \"\"\"\    "
            ],
            "description": "Python function"
    }
}

output

def  ():  
""" """   

Solution

  • Specify each line in a separate string of the body:

    You can choose the kind of doc string delimiters.

    The doc string should be indented (\t) and the body.

    {   
      "def": {
        "scope": "python",
        "prefix": "def func",
        "body": [
          "def $1 ():",
          "\t${2|\"\"\",''',\",'|} $3 $2",
          "\t$0"
        ],
        "description": "Python function"
      }
    }