关于VScode自动创建属于自己头文件
头文件的配置可以看一下下面这个地址,他写的很好
基础步骤
- 打开VScode,选择文件 -首选项-配置用户片段
- 点击弹出窗口新建 这里输入c.code
- 复制下面的代码粘贴
{
"CcHEADER":{
"scope": "c, cpp",
"prefix": "cpp",
"description": "header C/C++",
"body": [
"/*------------------------------------------------------------------------------",
" * @file ${TM_FILENAME/(.*)/${1:/upcase}/}",
" * @author FIRMWARE TEAM ",
" * @date $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
" * @brief THIS FILE PROVIDES ALL THE FIRMWARE FUNCTIONS.",
" * -----------------------------------------------------------------------------",
" * @attention ",
" ",
" * THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE ",
" * CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE ",
" * TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR ",
" * CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH ",
" * HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN ",
" * CONNECTION WITH PRODUCTS MADE BY CUSTOMERS. ",
" * ",
" * <H2><CENTER>© COPYRIGHT MINDMOTION </CENTER></H2> ",
"------------------------------------------------------------------------------*/",
"",
"/* Define to prevent recursive inclusion ------------------------------------*/",
"#define _${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_C_",
"",
"/* Files includes -----------------------------------------------------------*/",
"#include \"$TM_FILENAME_BASE.h\"",
"","",
"/*----------------------------------------------------------------------------*/",
"$0",
""
],
}
}
更改参数设置
"scope":"c,cpp"
是指的这个文件后缀是什么生效,这里是c,cpp
就是代表着我如果文件后缀为.c和.cpp
结尾就会有效,所以如果你改成你需要对应的语言后缀就可以了。prefix:"cpp"
是指前缀为cpp就会执行下面的内容,可以更改为自己想要的名字,作为启动,比如我这里举例更改为prefix:"lihua_main"
直接保存后,我们在文件中,现在创建一个.c文件(为什么是创建.c是因为第一步已经解释了),输入lihua
还没有输入完毕就会弹出下面的界面
- 所以到这里,你就定制了属于自己的启动方式和对应的代码后缀,现在我们加入自己一般写代码时加入的头文件,例如在学习中,我希望一打开就是只包含2个头文件
#include<stdio.h>和#include<string.h>
这2个头文件。
操作步骤:
1. 找到对应的位置(这个位置由你自己决定,可以自己尝试一下)
2. 输入"#include"<stdio.h>",
输入这个后就实现了效果。
由上面操作可以知道,""
是占一行位置,,
是做分隔。 - 然后还需要实现的效果是,我加载这些后,我希望光标是直接出现我想要的位置中。
1.$0
就是光标的文字,直接使用你发现,只能在左边,如果我想要在一个TAB健位置后出现光标
2. 使用\t
就可以实现一个TAB健位置
3. 最后输入"\t $0",
就可以实现在一个tab位置后出现光标 - 最后,加上上述条件,我这样输入以下代码
{
"CcHEADER":{
"scope": "c, cpp",
"prefix": "gcj_main",
"description": "header C/C++",
"body": [
"/*------------------------------------------------------------------------------",
" * @file ${TM_FILENAME/(.*)/${1:/upcase}/}",
" * @author FIRMWARE TEAM ",
" * @date $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
" * @brief THIS FILE PROVIDES ALL THE FIRMWARE FUNCTIONS.",
" * -----------------------------------------------------------------------------",
"#define _${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_C_",
"",
"/* Files includes -----------------------------------------------------------*/",
//"#include \"$TM_FILENAME_BASE.h\"",
//"#include \"$TM_FILENAME_BASE.h\"",
"#include <stdio.h>",
"#include <string.h>",
"","",
"/*----------------------------------------------------------------------------*/",
"int main()",
"{",
"\t $0",
"","","","",
" return 0;",
"}",
""
],
}
}
就可以实现下面的效果
总结:上面的操作难度很小,就是替换一下,但是我认为也算是一个小小的尝试,读者可以通过观察这些陌生的的东西进行百度,观察实现效果,这样远比与直接复制别人的配置文件来的舒服。笔者也刚刚准备踏入相关行业。所以解释或者操作可能很多都有问题和错误,希望得到各位前辈的建议和指正。