VSCode生成 自定义代码块

关于 VSCode生成 自定义代码块

如何新建

第一步: 文件 >> 首选项 >> 用户代码片段,新建一个全局的代码段,以下是本人新建的仅供参考:

{
	// 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:
	"TTCode": {
		"prefix": "JH_C_Des",
		"body": [
			"/**",
			"*  XXXXX",
			"*  @param  param    XXXXXX",
			"*  @return return   XXXXXX",
			"*  @author GJH,$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
			"*/"
		],
		"description": "Log output to console"
	},
	"c file header": {
		"prefix": "JH_C_H", //在新建立的页面中输入 H 就会有智能提示,Tab就自动生成好了
		"body": [
		  "/**                                                                             ",
		  "*  @file $TM_FILENAME",
		  "*  @author GJH",
		  "*  @brief 摘要.",
		  "*  @version 0.1",
		  "*  @date $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
		  "*  ",
		  "*/",
		  "",
		  "#ifndef $0_H",
		  "#define $0_H",
		  "#ifdef __cplusplus                                      ",
		  "extern \"C\"",
		  "{",
		  "#endif                                                                          ",
		  "/** INCLUDES -----------------------------------------------------------------*/",
		  "",
		  "",
		  //"#if $0",

		  "/** PRIVATEE INCLUDES --------------------------------------------------------*/",
		  "                                                                                ",
		  "/** PRIVATE DEFINES ----------------------------------------------------------*/",
		  "                                                                      ",
		  "/** EXPORTED TYPEDEFINES -----------------------------------------------------*/",
		  "                                                                        ",
		  "/** EXPORTED CONSTANTS -------------------------------------------------------*/",
		  "                                                                              ",
		  "/** EXPORTED MACROS ----------------------------------------------------------*/",
		  "",
		  "/** EXPORTED VARIABLES -------------------------------------------------------*/",
		  "",
		  "/** EXPORTED FUNCTIONS PROTOTYPES --------------------------------------------*/",
		  "                                                                                ",
		  "",
		  //"#endif \/*$0*/",
		  "",
		  "#ifdef __cplusplus                                            ",
		  "}/* extern \"C\" */",
		  "#endif                                                                          ",
		  "\n\n",
		  "#endif\/*$0*/                                                                 ",
		  "/******************************** End of file *********************************/"
		],
		"description": "A c file template." //用户输入后智能提示的内容(你可以用中文写“生成C++模板”)
	  },
	  "c file": {
		"prefix": "JH_C_C",
		"body": [
		  "/**                                                                             ",
		  "*  @file $TM_FILENAME",
		  "*  @author GJH",
		  "*  @brief 摘要.",
		  "*  @version 0.1",
		  "*  @date $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
		  "*  ",
		  "*/",
		  "",
		  "/** INCLUDES --------------------------------------------------------*/",
		  "#include \"$0\"",
		  " ",
		  //"#if $0",
		  " ",
		  "/** DEFINES --------------------------------------------------------*/",
		  " ",
		  "/** TYPEDEFS -------------------------------------------------------*/",
		  " ",
		  "/** STATIC PROTOTYPE -----------------------------------------------*/",
		  " ",
		  "/** STATIC VARIABLES -----------------------------------------------*/",
		  " ",
		  "/** MACROS  --------------------------------------------------------*/",
		  " ",
		  "/** GLOBAL FUNCTIONS -----------------------------------------------*/",
		  "\n",
		  "/** STATIC FUNCTIONS -----------------------------------------------*/",
		  "\n\n",
		  //"#endif \/*$0*/",
		  "/******************************** End of file *********************************/"
		],
		"description": "A c header file comment."
	  },
	  "multi-line comment": {
		"prefix": "JH_mc",
		"body": "/*\n * ${1:多行注释}\n */$0"
	  },
	  "single line comment": {
		"prefix": "JH_sc",
		"body": "/* ${1:单行注释} */$0"
	  },
	  "main()": {
		"prefix": "JH_main",
		"body": "int main(int argc, char const *argv[])\n{\n\t$0\n\treturn 0;\n}"
	  },
	  "For Loop": {
		"prefix": "JH_for",
		"body": "for (${1:int} ${2:i} = ${3:0}; ${2:i} ${4:<} ${5:count}; ${2:i}${6:++}) {\n\t$0\n}"
	  },
	  "Define and For Loop": {
		"prefix": "JH_dfor",
		"body": "int ${1:i};\nfor (${1:i} = ${2:0}; ${1:i} < ${3:count}; ${1:i}${4:++}) {\n\t$0\n}"
	  },
	  "else if": {
		"prefix": "JH_else_if",
		"body": "else if ($0) {\n\t\n}"
	  },
	  "case break": {
		"prefix": "JH_case",
		"body": "case $0:\n\t\n\tbreak;\n"
	  },
	  "printf": {
		"prefix": "JH_printf",
		"body": "printf(\"${1:%s}\\n\", $2);$3"
	  },
	  "array size": {
		"prefix": "JH_asize",
		"body": "sizeof(${1:arrayName})/sizeof($1[0])"
	  },
	  "define ARRAY_SIZE macro": {
		"prefix": "JH_defas",
		"body": "#define ARRAY_SIZE(${1:arrayName}) (sizeof($1)/sizeof($1[0]))\n$0"
	  },
	  "define MAX": {
		"prefix": "JH_defmax",
		"body": "#define MAX(x, y) ((x) > (y) ? (x) : (y))"
	  },
	  "define MIN": {
		"prefix": "JH_defmin",
		"body": "#define MIN(x, y) ((x) < (y) ? (x) : (y))"
	  },
	  "Header Include-Guard": {
		"prefix": "JH_once",
		"body": "#ifndef ${1:SYMBOL}\n#define $1\n\n$0\n\n#endif /* $1 */"
	  },
	  "Typedef": {
		"prefix": "JH_typedef",
		"body": "typedef ${1:int} ${2:MyCustomType};"
	  },
	  "#ifndef … #define … #endif": {
		"prefix": "JH_def",
		"body": "#ifndef ${1:SYMBOL}\n#define $1 ${2:value}\n#endif\t// ${1:SYMBOL}"
	  },
	  "#include <>": {
		"prefix": "JH_Inc",
		"body": "#include <${1:.h}>"
	  },
	  "#include \"\"": {
		"prefix": "JH_inc",
		"body": "#include \"${1:.h}\""
	  },
	 
	  "#pragma mark": {
		"prefix": "JH_mark",
		"body": "#if 0\n${1:#pragma mark -\n}#pragma mark $2\n#endif\n\n$0"
	  },
	  "fprintf": {
		"prefix": "JH_fprintf",
		"body": "fprintf(${1:stderr}, \"${2:%s}\\\\n\", $3);$4"
	  },
	  "scanf": {
		"prefix": "JH_scanf",
		"body": "scanf(\"${1:%s}\\n\", $2);$3"
	  },
	  "void": {
		"prefix": "JH_void",
		"body": "void ${1:func}($2) {\n\t$3\n}"
	  },
	  "any function": {
		"prefix": "JH_func",
		"body": "${1:int} ${2:func}($3) {\n\t$5\n\treturn ${4:0};\n}"
	  },
	  "write file": {
		"prefix": "JH_wf",
		"body": "FILE *${1:fp};\n${1:fp} = fopen (\"${2:filename.txt}\",\"w\");\nif (${1:fp}!=NULL)\n{\n\tfprintf(${1:fp},\"${3:Some String\\\\n}\");\n\tfclose (${1:fp});\n}"
	  },
	  "read file": {
		"prefix": "JH_rf",
		"body": "FILE *${1:fp};\n${1:fp} = fopen (\"${2:filename.txt}\",\"r\");\nif (${1:fp}!=NULL)\n{\n\tfscanf(${1:fp},\"${3:Some String\\\\n}\", ${3:&var});\n\tfclose (${1:fp});\n}",
		"description": "read file opeartion including fopen, fscanf and fclose."
	  }
}

代码片段中的内置变量

TM_SELECTED_TEXT   当前选定的文本或空字符串
TM_CURRENT_LINE    当前行的内容
TM_CURRENT_WORD    光标下的内容或空字符串
TM_LINE_INDEX    从零开始的当前行号
TM_LINE_NUMBER    从一开始的当前行号
TM_FILENAME    当前文档的文件名
TM_FILENAME_BASE    基当前文档的文件名,不带扩展名
TM_DIRECTORY    当前文档的目录
TM_FILEPATH    当前文档的完整文件路径
CLIPBOARD    剪贴板的内容
WORKSPACE_NAME    打开的工作区或文件夹的名称

日期:
CURRENT_YEAR    当前年份
CURRENT_YEAR_SHORT    当前年份的最后两位数字
CURRENT_MONTH    两位数的当前月份(例如“02”)
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    当前秒
CURRENT_SECONDS_UNIX    自格林威治时间以来的秒数(1970年1月1日)
 
注释:
BLOCK_COMMENT_START    区块注释开始 <!--
BLOCK_COMMENT_END    区块注释结束 -->
在php中会变成/*和*/
LINE_COMMENT    单行注释: PHP中: //

文字转换(正则)
${TM_FILENAME/[\\.]/_/}    将文件名中的.换成_
${TM_FILENAME/[\\.-]/_/g}    将文件名中的.和-换成_
${TM_FILENAME/(.*)/${1:/upcase}/}    将文件名完全转为大写
${TM_FILENAME/[^0-9^a-z]//gi}    删除非字母和数字的字符,包括符号
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值