关于 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} 删除非字母和数字的字符,包括符号