vscode添加自定义代码片段snippet

参考目录
掘金教程
配置参数教程
在线生成snippets代码

一些常用代码块经常需要重复书写,怎么解决痛点呢,这时候snippet是个解药。配置后只需输入自定义的key即可生成预设内容.

添加步骤
  • code -> Preferences -> User Snippets
  • 选择New Snippets新建 或者 Exiting Snippets修改
  • 会打开 名称.code-snippets 文件,在此文件进行配置
  • 使用时,直接输入 .snippets文件的 prefix值 即可使用片段
.snippet文件一览

配置文件里可以使用一些变量,最常用的如 1 光标首先指向位置, {1}光标首先指向位置, 1光标首先指向位置,{1}按Tab键后光标下一个位置,${0}光标最后位置。然后还有一些变量可以使用,如文件path当前时间等

{
	// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
	// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
	// is left empty or omitted, the snippet gets applied to all languages. 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": {
	// 	"scope": "javascript,typescript",
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"myvuepage": {
		"scope": "vue",
		"prefix": "myvuepage",
		"body": [
		  "<template>",
		  "  <div class='${1}'>",
		  "    ${0}",
		  "  </div>",
		  "</template>",
		  "",
		  "<script>",
		  " export default {",
		  "   name: \"${TM_FILENAME_BASE/(.)/${1:/upcase}/}\",",
		  "   components: {",
		  "   },",
		  "   props: {",
		  "   },",
		  "   data(){",
		  "     return {",
		  "     } ",
		  "   },",
		  "   computed: {",
		  "   },",
		  "   watch: {",
		  "   },",
		  "   created() {",
		  "   },",
		  "   methods: {",
		  "   },",
		  " }",
		  "</script>",
		  "",
		  "<style scoped>",
		  "  ${2}",
		  "</style>"
		],
		"description": "初始化一个 vue 文件"
	},
	"myfileurl": {
		"scope": "",
		"prefix": "myfileurl",
		"body": ["/**", "${TM_DIRECTORY}", "${1:${TM_FILEPATH}}$0", "*/"],
		"description": "查看当前文件路径"
	},
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值