VSCODE如何使用代码片段

本文详细介绍如何在VSCode中配置和使用代码片段,包括创建自定义代码模板、快速插入常用代码块的方法,并提供示例展示如何为C语言配置文件头、函数声明等常见代码片段。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

vscode中如何添加代码片段,插入代码片段语法主要是JSON语法结构


一、打开命令面板

使用快捷键Ctrl+shift+p打开,在面板中找到首选项配置代码片段
在这里插入图片描述
选择语言,这里我选择c-plus.code-snippet

二、修改增加代码片段

示例:
{
// 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’);”,
// “KaTeX parse error: Expected 'EOF', got '}' at position 60: …o console" // }̲ "h1": { "pr…BLOCK_COMMENT_START*”, //注释块开始
" * File Name: $TM_FILENAME", //文件名
" ",
" * Copyright © 公司名称
" ",
" * Author: 输入作者 “,
" “,
" * Date: C U R R E N T Y E A R / CURRENT_YEAR/ CURRENTYEAR/CURRENT_MONTH/$CURRENT_DATE”,
" “,
" * Project: W O R K S P A C E N A M E " , " ∗ " , " ∗ D e s c r i p t i o n : " , " ∗ " , " ∗ H I S T O R Y " , " ∗ " , " ∗ WORKSPACE_NAME", " *", " * Description:", " *", " * HISTORY", " *", " * WORKSPACENAME","","Description:","","HISTORY","","BLOCK_COMMENT_END”, //注释块结束
“/**”,
"
@file $TM_FILENAME", //文件名
"
”,
"
@brief ",
"
",
“*****************************************************************************/”,

	"/******************************************************************************",
	"* 0.#include declarations",
   "*****************************************************************************/",
   "$0",
	"/******************************************************************************",
	"* 1.local macro definitions",
	"*****************************************************************************/\n",
	"/******************************************************************************",
	"* 2.local type definitions",
	 "*****************************************************************************/\n",
	"/******************************************************************************",
	"* 3.local variable declarations",
	"*****************************************************************************/\n",
	"/******************************************************************************",
	"* 4.local function prototypes",
	 "*****************************************************************************/\n",
	"/******************************************************************************",
	"* 5.function define",
	 "*****************************************************************************/\n",
	],
	"description": ".c头文件描述说明"
  },
  
  "h2": {
	"prefix": "h2",
	"body": 
	["$BLOCK_COMMENT_START*", 
	" * File Name: $TM_FILENAME ",
	" *",
	" * Copyright (C) 公司名称,
	" *",
	" * Author:  作者名称 ",
	" *",
	" * Date: $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE", //时间日期
	" *",
	" * Project: $WORKSPACE_NAME", //工作工程名称
	" *",
	" * Description:",
	" *",
	" * HISTORY",
	" *",
	" *$BLOCK_COMMENT_END",
	"#ifndef __${TM_FILENAME_BASE/${1:/upcase}/}_H__",
	"#define __${TM_FILENAME_BASE/${1:/upcase}/}_H__\n",
	"#if defined (__cplusplus)",
	"extern \"C\" {",
	"#endif /* __cplusplus */",
	"$0\n\n\n\n\n\n",
	"#if defined (__cplusplus)",
    "}",
	"#endif /* __cplusplus */\n",
	"#endif\n",
    ],
	"description": ".h头文件描述说明"
  }, 

  "f1":{
	"prefix": "f1",  
	"body": [
		"/**************************************************************************",
		"* @name : $CLIPBOARD ",    
		"* @brief :      ",
		"* @param[in] :  ",
		"* @return :     ",
		"**************************************************************************/",
		"$0",
	],
	"description": "函数描述说明"
  }, 

  "m1":{
	"prefix": "m1",  
	"body": [
        "/**< $0 */",
	],
	"description": "结构体成员描述"
  }, 

  "bf":{
	"prefix": "bf",  
	"body": [
        "/**",
		"* @brief $0",
		"*/",
	],
	"description": "结构体描述"
  }, 

}
编辑后保存

三、使用代码片段

在vscode中新建立一个.c或.h文件,
然后输入h1,自动会弹出提示代码补充提示,按tab键,自动输入h1代表的代码片段。
在这里插入图片描述

四、编辑关键字说明

参考网址:https://code.visualstudio.com/docs/editor/userdefinedsnippets

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值