vscode snippets

satya - 2/11/2024, 3:27:06 PM

Vscode docs on snippets

Vscode docs on snippets

satya - 2/11/2024, 3:28:50 PM

Stupid thing!!!

  1. There are instructions in the snippet file
  2. If you leave them as they are..and add your snippet under it won't work
  3. Delete the comments
  4. Just use the snippet block

satya - 2/11/2024, 3:29:59 PM

The file will be saved under


your folder/.vscode/file.code-snippets

satya - 2/11/2024, 3:30:12 PM

An example


{
    "Block Comments": {
        "scope": "python",
      "prefix": "block_comments",
        "body": [
            "\"\"\"",
            "*************************************************",
            "* Prep the template",
            "*************************************************",
            "\"\"\""
        ],
        "description": "Inserts a block comment for template preparation."
    }
}

satya - 2/11/2024, 3:31:24 PM

Note the global files are stored under


/users/user/appdata/roaming/code/user/snippets/filenam.code-snippets

satya - 2/11/2024, 3:41:48 PM

Here is how you put 2 snippets


{
    "Block Comments": {
        "scope": "python",
      "prefix": "block_comments",
        "body": [
            "\"\"\"",
            "*************************************************",
            "* Prep the template",
            "*************************************************",
            "\"\"\""
        ],
        "description": "Inserts a block comment for template preparation."
    },
   "lib_main_snippet": {
         "prefix": "lib_main_snippet",
         "body": [
            "def test():",
            "    pass",
            "",
            "def localTest():",
            "    log.ph1(\"Starting local test\")",
            "    test()",
            "    log.ph1(\"End local test\")",
            "",
            "if __name__ == '__main__':",
            "    localTest()"
         ],
         "description": "Template for local testing with a main block"
      }
}

satya - 2/11/2024, 3:43:46 PM

The json format of this file is important

  1. A single root note
  2. Each snippet is a dictionary entry
  3. Not sure if comments are allowed
  4. Even a small mistake, vscode reports no snippets available...
  5. The whole error messaging could be quite a case in-convalescence.

satya - 2/11/2024, 3:46:39 PM

How to create this snippet file

  1. ctrl-shift-p and "snippets"
  2. use "configure user snippets"
  3. Instead of choosing global you may want to chose your local snippet
  4. this file is saved in .vscode/file.code-snippet
  5. I assume you can have multiple snippet files
  6. Doing locally gives you more access and control to that file where as the global files are tucked away deep inside python global jungle.

satya - 2/11/2024, 3:47:41 PM

How do I invoke or insert the snippet

  1. Just type the beginning words
  2. intellisense should prompt you right away
  3. No tab is needed
  4. No ctrl-space is needed

satya - 2/11/2024, 3:49:10 PM

You have the option in settings to

  1. Go to settings (ctrl-shift-p: search for settings)
  2. Editor: snippet suggestion
  3. options: top, bottom, inline (default), none
  4. I chose "top"
  5. works good so far