vscode自定义代码片段

最近开始用vscode,发现还是挺好用的,微软开发的轻量级开源IDE,支持多平台,插件也很丰富。下面说一个今天get到的技能。设置vscode自定义代码片段,让撸码更快捷。

操作步骤

  • 首先按下快捷键Ctrl+P
  • 输入>snippet,选择“首选项:配置用户代码片段”
  • 输入php,选择php.json
  • 在打开的php.json文件中,编辑模版

编辑模版示例

<code class="language-null">{
    // Place your snippets for php 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"
    // }

    "Print to debug": {
        "prefix": "dd",
        "body": [
            "echo Debug::vars(\\$ddd$0);"
        ],
        "description": "echo log"
    },
    "json request": {
        "prefix": "json",
        "body": [
            "\\$this-&gt;check_view(array('json'));",
            "\\$params = \\$this-&gt;arg_filter(\\$this-&gt;request-&gt;post$0(), array('code'));",
            "\\$v = Validation::factory(\\$params);",
            "\\$v-&gt;rule('code', 'not_empty');",
            "\\$this-&gt;check_valid(\\$v);",
            "\n",
            "\\$ret = \\$this-&gt;itg_interface(array(",
            "\t'parent_code' =&gt; \\$params['code'],",
            "));",
            "\\$this-&gt;view_data = \\$this-&gt;convert_output(\\$ret);"
        ]
    }
}
</code>

注意点

  • 除了php还可以选其他语言
  • prefix表示输入的内容,按tab键后会出现body中的内容
  • $0表示光标位置
  • body中的每一项,表示一行
  • $这种特殊符号要转义,tab要用\t替代,换行要用\n替代,

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>