vscode snippets简介

背景

日常开发中很多模板代码需要反复写,可以通过snippets来简化

使用

此处使用go举例
ctrl+shift+p打开面板,然后选择配置用户代码片段,然后选择go,出现go,json文件,在其中输入如下内容

{
    // Place your snippets for go 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.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }
    "main": {
        "prefix": "main",
        "body": [
            "package main",
            "func main() {",
                "$1",
            "}",
        ],
        "description": "log info"
    }
}

新建一个demo.go文件并用vscode打开,输入main然后tab,就会得到如下内容

package main

func main() {

}

介绍

$1 $2是占位符,tab切换的顺序

${1:defaultvalue}可以设置默认值

{2:$1}可以达到联动修改的效果

可选值

${1|value1,value2|}
占位符1处可以选择value1或者value2值

变量和转换详见官方文档

Snippets in Visual Studio Code

你可能感兴趣的:(vscode snippets简介)