只需看完官方文档第一节 https://code.visualstudio.com/docs/editor/userdefinedsnippets,基本就能把 snippet 用起来了

snippet 插件安装

在插件面板中搜索:@category:"snippets",即可查找各种语言的 snippet 插件。

有语言服务器支持的插件一般都自带 snippet,如ms-vscode.cpptools, golang.go, ms-python.pythonredhat.java等等,如果已安装此类插件,则无需再安装 snippet 了

设置项: Ctrl + , 打开设置页面,点击右上角打开 json 设置文件

1
2
3
{
    "snippet.verbose": true,
}

配置个人 snippet

在命令面板 Ctrl+Shift+P 中,输入 snippet,选择 Preferences: Configure User Snippets,再选择自己想要配置 snippet 的语言即可。

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
  // Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
  // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
  // same ids are connected.
  "using std common": {
    "prefix": "uns",
    "body": [
    "#include <${1:string}>",
    "#include <${2:vector}>",
    "",
    "using namespace std;",
    ""
    ],
    "description": "2 basic heads"
  },
  "unordered_map": {
    "prefix": "inm",
    "body": [
    "#include <${1:unordered_map}>",
    ""
    ],
    "description": ""
  },
  "unordered_set": {
    "prefix": "ins",
    "body": [
    "#include <${1:unordered_set}>",
    ""
    ],
    "description": ""
  },
  "container": {
    "prefix": "ini",
    "body": [
    "#include <${1:iostream}>",
    ""
    ],
    "description": ""
  },
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
    "HEADER": {
        "prefix": "header",
        "body": [
            "#!/usr/bin/python3",
            "# -*- encoding: utf-8 -*-",
            "'''",
            "@File    :   $TM_FILENAME",
            "@Time    :   $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
            "@Author  :   younger ",
            "@Version :   1.0",
            "@Contact :   7650421@qq.com",
            "@WebSite :   younger-1.github.io",
            "'''",
            "",
            "$0"
        ],
    },
    "Deque": {
        "prefix": "fci",
        "body": [
            "from collections import ${1:deque}",
            ""
        ]
    },
    "Numpy": {
        "prefix": "inn",
        "body": [
            "import numpy as np",
            "import matplotlib.pyplot as plt",
            ""
        ]
    },
}