(VS Code) 自定义代码片段 Snippets

6 篇文章 0 订阅

(VS Code) 自定义代码片段 Snippets

操作入口

快捷键

使用快捷键 Ctrl + Shift + P

输入Snippets:Configure User Snippets

在这里插入图片描述

在这里插入图片描述

导航栏

文件 -> 首选项 -> 配置用户代码片段

在这里插入图片描述

片段种类

  • 全局代码片段
  • 当前文件夹的代码片段
  • 指定语言的代码片段

默认信息

{
	// Place your snippets for c 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"
	// }
}
// 片段名称
"": {
  // 触发词
  "prefix": "",
  // 片段内容
  "body": [
    ""
  ],
  // 片段描述
  "description": ""
}

编辑

有占位符的时候,可以通过tab键进行编辑

常规

"快速幂": {
  "prefix": "binpow",
  "body": [
    "int binPow(int base, int expo, int mod) {",
    "    int ans = 1;",
    "    base %= mod;",
    "    if (expo == 0) {",
    "        ans = 1 % mod;",
    "    } else {",
    "        while (expo) {",
    "            if (expo & 1) {",
    "                ans = ans * base % mod;",
    "            }",
    "            base = base * base % mod;",
    "            expo >>= 1;",
    "        }",
    "    }",
    "    return ans;",
    "};"
  ],
  "description": "快速幂"
}

占位符-无默认

"占位符-无默认": {
  "prefix": "forr",
  "body": [
    "    for (int $1 = 0; $1 < $2; $1 += 1) {",
    "        ",
    "    }"
  ],
  "description": "占位符-无默认"
}

占位符-有默认

"占位符-有默认": {
  "prefix": "forr",
  "body": [
    "    for (int ${1:i} = 0; ${1:i} < ${2:n}; ${1:i} += 1) {",
    "        ",
    "    }"
  ],
  "description": "占位符-有默认"
}

辅助工具

snippet generator (snippet-generator.app)

在这里插入图片描述

在这里插入图片描述




END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值