最近开始用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->check_view(array('json'));", "\\$params = \\$this->arg_filter(\\$this->request->post$0(), array('code'));", "\\$v = Validation::factory(\\$params);", "\\$v->rule('code', 'not_empty');", "\\$this->check_valid(\\$v);", "\n", "\\$ret = \\$this->itg_interface(array(", "\t'parent_code' => \\$params['code'],", "));", "\\$this->view_data = \\$this->convert_output(\\$ret);" ] } } </code>
注意点
- 除了php还可以选其他语言
- prefix表示输入的内容,按tab键后会出现body中的内容
$0
表示光标位置- body中的每一项,表示一行
- $这种特殊符号要转义,tab要用\t替代,换行要用\n替代,