美文网首页
vscode snippets简介

vscode snippets简介

作者: wwq2020 | 来源:发表于2022-01-13 16:17 被阅读0次

背景

日常开发中很多模板代码需要反复写,可以通过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}可以设置默认值

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

可选值

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

变量和转换详见官方文档

Snippets in Visual Studio Code

相关文章

网友评论

      本文标题:vscode snippets简介

      本文链接:https://www.haomeiwen.com/subject/yewncrtx.html