在 Visual Studio Code 中添加自定义的代码片段

无论是那个编辑器,如果能够添加一些自定义代码片段,能够大大提升代码的输入效率。

本文介绍如何在 Visual Studio Code 中添加自定义代码片段。


Visual Studio Code 的代码片段设置

你可以在 Visual Studio Code 的菜单中找到代码片段的设置入口,在 File -> Preferences -> User Snippets 中。

打开用户代码片段设置
▲ 打开用户代码片段设置

点开后,会让你选择做什么:

  • 新建全局代码片段
  • 新建适用于当前工作区的代码片段
  • 新建特定于语言的全局代码片段

选择代码片段范围

根据你的需要选择一个范围:

  • 比如你需要在任何文件中都能够使用的代码片段,那么选择全局代码片段。
  • 比如你需要仅在当前工作区生效的代码片段(例如我写博客是才会用到的博客片段),那么选择工作区代码片段。
  • 如果是特定于语言的,那么选择自己需要的语言。

在新建全局代码片段和当前工作区的代码片段的时候,是需要自己指定名称的。

指定代码片段的名称
▲ 指定代码片段的名称

编写代码片段

无论你使用哪种方式新建代码片段,Visual Studio Code 都会帮你打开这个代码片段文件。整个文件一开始是被注释的状态,就像下面这样:

{
   
	// 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"
	// }
}

上面的注释,翻译一下是这样的:

可以将你的全局代码片段放到这里。每一个代码片段都由一个名称来定义,其值包含此代码片段的作用域(scope)、前缀(prefix)、代码片段的内容(body)与其描述信息(description)组成。

  • scope 字段中填写以逗号分隔的作用域 Id,如果 scope 字段为空
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Visual Studio Code自定义背景颜色,可以按照以下步骤进行操作: 1. 首先,在Visual Studio Code编辑器点击左侧的扩展按钮,或者使用快捷键Ctrl+Shift+X来打开扩展面板。 2. 在扩展面板搜索并安装名为"background-cover"的插件。 3. 安装完成后,进入File->Preferences->Settings来打开设置面板。 4. 在设置面板,使用搜索框搜索"background",然后点击"Edit in settings.json"来编辑配置文件。 5. 在打开的settings.json文件,可以看到一个示例配置。在"background.customImages"的数组,可以添加自定义的背景图片路径,比如"file:///D:/3.jpg",其"D:/3.jpg"是图片存放的地址。 6. 如果只想更改背景颜色,可以在Visual Studio Code编辑模式下,依次按下Ctrl+K,然后按下Ctrl+T,即可预览和设置背景颜色。 通过以上步骤,你可以在Visual Studio Code自定义背景颜色或背景图片。希望以上信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [自定义更改Visual Studio Code的背景图和背景颜色的详细过程](https://blog.csdn.net/lsxlyy/article/details/115377455)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Visual Studio Code 自定义面板背景图](https://blog.csdn.net/weixin_51290138/article/details/130690065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Visual Studio 自定义代码片段的方法](https://download.csdn.net/download/weixin_38553648/12727924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值