当前位置: 首页 > news >正文

vscode自定义代码片段

前言

代码片段,指的是能够帮助输入重复代码模式,比如初始页面的模板。通过 snippet ,我们仅仅输入一小段字符串,就可以在代码片引擎的帮助下,生成预定义的模板代码,接着我们还可以通过在预定义的光标位置之间跳转,来快速补全模板。
在这里插入图片描述

流程

首先,进入代码片段设置文件

有三种方法:

  • 通过快捷键「Ctrl + Shift + P」打开命令窗口(All Command Window),输入「snippet」,点选「首选项:配置用户代码片片段」
    在这里插入图片描述

  • 点击界面最左侧竖栏(也即活动栏)最下方的齿轮按钮,在弹出来的菜单中点选「用户代码片段」
    在这里插入图片描述

  • 通过文件 > 首选项 > 用户代码片段

配置json文件

{// Place your 全局 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"// }
}

基本语法说明

  1. scope:域。代码片适用的「语言」(可选),为空意为适用所有语言
  2. prefix :触发快捷提示的字符串前缀
  3. body :代码片段主体
    • \$num 是每次按 tab 键光标移动对位置,\$0 表示光标最后停留位置,不设置 $0,这光标最终位置在文件末尾;
    • \${2:默认文本} 跳转到指定位置到同时选中默认文本,方便修改;
    • $CURRENT_YEAR是引用的 snippets 内置变量,其它还有:
    • TM_FILENAME 当前文件名
    • TM_FILENAME_BASE 当前文件名,不带扩展名
    • TM_DIRECTORY 当前文件所属目录的绝对路径
    • TM_FILEPATH 当前文件的绝对路径
    • CURRENT_YEAR 当前年份
    • CURRENT_YEAR_SHORT 当前年份,最后两位数字
    • CURRENT_MONTH 当前月份数字形式,两位表示
    • CURRENT_MONTH_NAME 当前月份英文形式,如 July
    • CURRENT_MONTH_NAME_SHORT 当前月份英文缩写形式,如 Jul
    • CURRENT_DATE 当前日
    • CURRENT_DAY_NAME 当前星期,如 Monday
    • CURRENT_DAY_NAME_SHORT 当前星期缩写形式,如 Mon
    • CURRENT_HOUR 当前小时,24小时格式,两位表示
    • CURRENT_MINUTE 当前分钟,两位表示
    • CURRENT_SECOND 当前秒,两位表示
    • \n 换行
    • \t 制表符
  4. description :快捷提示窗对该代码片段对描述

举例

这里以前言中的 “qbr” 举例

{// Place your 全局 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:"create react component": {// "scope": "javascript,typescript","prefix": "qbr","body": ["import { useState } from \"react\"","","const ${1:Demo} = () => {","\treturn <div>$2</div>","}","export default ${1:Demo}"],"description": "Log output to console"}
}

效果
输入快捷键qbr,回车后生成代码片段如下
由于${1:Demo}包裹了两处,默认会同时选中,便于修改
修改完毕后按下Tab键,由于$2,光标移入到第4行的div标签中

在这里插入图片描述

http://www.lryc.cn/news/266302.html

相关文章:

  • 【贪心算法】专题练习一
  • 【JMeter】使用nmon进行性能资源监控
  • Unity预设体
  • Elasticsearch 写入优化探索:是什么影响了refresh 耗时?
  • Java8新特性——函数式接口
  • Epson打印机连接wifi
  • Chapter 7 - 6. Congestion Management in Ethernet Storage Networks以太网存储网络的拥塞管理
  • 【论文笔记】NeuRAD: Neural Rendering for Autonomous Driving
  • 通信原理 | 分贝dB、功率、功率谱、功率谱密度、信噪比
  • Go中的Context是什么?
  • 碳排放预测 | 基于ARIMA和GM(1,1)的碳排放预测(Matlab)
  • FPFA.一种二倍频电路代码描述以及测量详情
  • dotnet命令创建C#项目,VSCode打开
  • 在GitHub找开源项目
  • GAMES101-LAB1
  • Docker 编译OpenHarmony 4.0 release
  • Vue 3 表单处理精讲:打造响应式注册表单的艺术
  • 浅谈Guava Cache的参数使用
  • 交通流预测 | Matlab基于KNN-BiLSTM的交通流预测(对比SVR、LSTM、GRU、KNN-LSTM)
  • 云卷云舒:面向业务的智能运维(上)
  • centos 7.4 docker
  • 零基础学人工智能:TensorFlow 入门例子
  • go从0到1项目实战体系二一:gin框架安装
  • 运用JavaSE知识实现图书管理系统
  • 微信小程序生成一个天气查询的小程序
  • Seata源码——TCC模式解析02
  • 缓存-Redis
  • PADS Layout安全间距检查报错
  • ebpf基础篇(二) ----- ebpf前世今生
  • 我的一天:追求专业成长与生活平衡