LV02-01-用户代码片段.md

本文主要是VSCode用户自定义代码片段相关笔记,不区分平台,不管是win还是linux中的VScode均可使用。若笔记中有错误或者不合适的地方,欢迎批评指正😃。

点击查看使用工具及版本
PC端开发环境 Windows Windows11
Ubuntu Ubuntu20.04.6的64位版本(一开始使用的是16.04版本,后来进行了升级)
VMware® Workstation 17 Pro 17.0.0 build-20800274
终端软件 MobaXterm(Professional Edition v23.0 Build 5042 (license))
Win32DiskImager Win32DiskImager v1.0
Linux开发板环境 Linux开发板 正点原子 i.MX6ULL Linux 阿尔法开发板
uboot NXP官方提供的uboot,NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)
linux内核 linux-4.15(NXP官方提供)
点击查看本文参考资料
参考资料 相关链接
VS Code官网Visual Studio Code - Code Editing. Redefined
VS Code历史版本Visual Studio Code (历史版本下载)
VS Code官方文档Getting Started
工作区相关说明Workspaces in Visual Studio Code
点击查看相关文件下载
--- ---

一、代码段

1. 打开相关文件

我们首先肯定需要打开自定义代码段设置啦,我们打开自己想要配置的编程语言的自定义代码片段的定义文件,可以从这里打开:

image-20220424185124770

然后会在顶栏看到如下选项:

image-20220424185212371

选中自己想要配置的编程语言就可以啦,我这里选的是 C 语言的,然后便会打开一个空的配置文件,里边的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}

上边的是啥?我们后边再解释。

2. 代码段作用域

从上边的图中,我们看到有三类选项,一类是各种文件类型,一类是新建 “DirName” 代码片段,还有一类是全局代码片段。这些选项新建出来的自定义代码片段文件语法以及内容都是一样的,只不过作用域不同罢了。它们最终生成的代码片段文件都将存于以下目录中:

1
2
3
4
5
6
# 全局作用域和特定文件类型作用域 类型的自定义代码片段
Windows下在C:\Users\username\AppData\Roaming\Code\User\snippets
Linux下在:~/.config/Code/User/snippets

# 文夹件作用域 类型的自定义代码片段(工作区中的.vscode目录)
./.vscode
  • 全局作用域

这种类型自定义代码片段是全局生效的,相当于是创建在 VScode 软件内部的文件。不管是哪个工作区,哪种文件,只要是在这个软件中打开的文件,都会有效。

  • 特定文件类型作用域

这种类型的定义代码片段跟全局作用域的文件路径是一致的,都是创建在了 VS Code 中,会一直存在。但是这种代码块只适合于指定的文件类型。

  • 文件夹作用域

这种类型的自定义代码片段是创建在某个文件下 .vscode 这个隐藏文件夹中的,这个代码块只适用于当前文件夹,出了这个文件夹这个代码片段文件就失效了。

3. 书写说明

先来看一下官方的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
Print to console 自定义代码段的名称。
scope 代码片段的作用文件类型,这里我们可以指定文件类型,多种类型之间用逗号隔开。
prefix 触发代码块的字符串。输入这里的字符串后就会产生提示,选中后按下Tab就可以自动替换为自定义的代码片段。
body 代码片段的主体内容,就是我们要插入的内容格式。
description 代码片段描述。
$0 表示光标最终位置。
$1, $2, ... $1 表示使用代码块敲击回车或者 tab 键后光标定位的位置。
$2 $3 $4…表示我们按下tab光标依次出现的位置。

【注意】

(1)每个字符串元素就代表一行,行与行之间用 , 隔开表示换行。或者使用 \n 换行。

(2)行内不能使用 tab 键缩进,只能使用空格 <space> 或者 \t 表示缩进。

3. 预定义变量

类似于上边 $1 和 $2 这种,是 VS Code 内部自己定义的变量,我们可以直接引用。

点击查看常见预定义变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
文件相关
TM_SELECTED_TEXT 当前选定的文本或空字符串;
TM_CURRENT_LINE:当前行的内容;
TM_CURRENT_WORD:光标所处单词或空字符串
TM_LINE_INDEX:行号(从零开始);
TM_LINE_NUMBER:行号(从一开始);
TM_FILENAME:当前文档的文件名;
TM_FILENAME_BASE:当前文档的文件名(不含后缀名);
TM_DIRECTORY:当前文档所在目录;
TM_FILEPATH:当前文档的完整文件路径;
时间相关
CURRENT_YEAR: 当前年份;
CURRENT_YEAR_SHORT: 当前年份的后两位;
CURRENT_MONTH: 格式化为两位数字的当前月份,如 02
CURRENT_MONTH_NAME: 当前月份的全称,如 July;
CURRENT_MONTH_NAME_SHORT: 当前月份的简称,如 Jul;
CURRENT_DATE: 当天月份第几天,如 08
CURRENT_DAY_NAME: 当天周几,如 Monday;
CURRENT_DAY_NAME_SHORT: 当天周几的简称,如 Mon;
CURRENT_HOUR: 当前小时(24 小时制);
CURRENT_MINUTE: 当前分钟;
CURRENT_SECOND: 当前秒数。
CLIPBOARD:当前剪贴板中内容。

更多的也可以查看官方文档的这一节:Snippets in Visual Studio Code

一般使用格式如下:

1
2
3
$Name
或者
${Name}

4. 变量使用前的编辑

这一部分可以参考这里:Snippets in Visual Studio Code——Transform examples

这里根据官方文档,以文件 example-123.456-TEST.js 为例:

Example Output Explanation
"${TM_FILENAME/[\\.]/_/}" example-123_456-TEST.js Replace the first . with _
"${TM_FILENAME/[\\.-]/_/g}" example_123_456_TEST_js Replace each . or - with _
"${TM_FILENAME/(.*)/${1:/upcase}/}" EXAMPLE-123.456-TEST.JS Change to all uppercase
"${TM_FILENAME/[^0-9^a-z]//gi}" example123456TESTjs Remove non-alphanumeric characters

二、开启补全功能

接下来就是开启相关的功能了,我们可以去设置中打开 settings.json ,注意这里其实是可能有三个这样的文件的,如果有创建的话,一个应该是位于当前目录下的 .vscode (不管是 win 还是 Linux 都是)目录下,这个配置文件的优先级会高于另外一个,但是这里的不能用 VScode 自带的同步功能进行同步。另一个配置文件是可以通过 VScode 自动进行同步的,在 win 下一般位于 C:\Users<username>\AppData\Roaming\Code\User ,在 Linux 中一般位于 ~/.config/Code/User 中。还有一种是如果保存了工作区的话,可以创建一个工作区配置文件。我一般是喜欢用第二种,因为可以同步,换了电脑同步一下就可以啦。

那怎么打开呢?按下 Ctrl + Shift + p 即可打开设置搜索窗口,输入 settings.json 会有下图提示:

iimage-20220425185212371

打开文件后,按照语法格式添加以下内容(我用 C 语言较多,这里我就只配置了 C ):

1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[c]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},

三、我的常用代码段

1. C语言

1. c.json

点击查看 c.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"head": {
"prefix": "head", // 触发的关键字 输入 head 按下tab键
"body": [
"$BLOCK_COMMENT_START* =====================================================",
" * Copyright © hk. 2022-2025. All rights reserved.",
" * File name : ${TM_FILENAME}",
" * Author : 上上签",
" * Date : $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
" * Version : ",
" * Description: ",
" * ======================================================",
" $BLOCK_COMMENT_END",
"",
],
"description": "Description at the beginning of file!"
},
"main": {
"prefix": "main", // 触发的关键字 输入 main 按下tab键
"body": [
"#include <stdio.h>",
"",
"int main(int argc, const char *argv[])",
"{",
" /* code */ ",
" ", // $1 表示输入触发关键字后,按下 Tab 后光标定位到的第一个位置
"",
" return 0;",
"}",
],
"description": "main function!"
},
"mainh": {
"prefix": "mainh", // 触发的关键字 输入 mainh 按下tab键
"body": [
"#ifndef __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__",
"#define __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__",
"",
"#endif $BLOCK_COMMENT_START __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__ $BLOCK_COMMENT_END",
""
],
"description": ""
},
"func": {
"prefix": "func", // 触发的关键字 输入func按下tab键
"body": [
"$BLOCK_COMMENT_START*",
" * @brief ",
" * @note ",
" * @param ",
" * @retval ",
" $BLOCK_COMMENT_END", // $1 表示输入触发关键字后,按下 Tab 后光标定位到的第一个位置
],
"description": ""
},
"desc": {
"prefix": "desc", // 触发的关键字 输入desc按下tab键
"body": [
"/** $0 */",
],
"description": ""
},
}

1.2 settings.json

settings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[c]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},

2. ARM

2.2 arm.json

点击查看 arm.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
// Place your snippets for arm here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"head": {
"prefix": "head", // 触发的关键字, 输入后按下tab键
"body": [
"$BLOCK_COMMENT_START* =====================================================",
" * Copyright © hk. 2022-2025. All rights reserved.",
" * File name: ${TM_FILENAME}",
" * Author : 上上签",
" * Date : $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
" * Version : ",
" * Description: ",
" * ======================================================",
" $BLOCK_COMMENT_END",
"$0",
],
"description": "Description at the beginning of file"
},
"desc": {
"prefix": "desc", // 触发的关键字, 输入后按下tab键
"body": [
"$BLOCK_COMMENT_START*",
" * Description: ",
" $BLOCK_COMMENT_END",
"$1",
],
"description": "Description!"
},
}

2.2 settings.json

settings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[arm]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},

3. C++

3.1 cpp.json

这部分配置目前还主要是针对 .h 文件,不清楚为啥,这个文件被归入了 cpp ,所以就在这里添加常用代码段啦。

点击查看 cpp.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"head": {
"prefix": "head", // 触发的关键字 输入 head 按下tab键
"body": [
"$BLOCK_COMMENT_START* =====================================================",
" * Copyright © hk. 2022-2025. All rights reserved.",
" * File name : ${TM_FILENAME}",
" * Author : 上上签",
" * Date : $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
" * Version : ",
" * Description: ",
" * ======================================================",
" $BLOCK_COMMENT_END",
"",
],
"description": "Description at the beginning of file!"
},
"main": {
"prefix": "main", // 触发的关键字 输入 main 按下tab键
"body": [
"#include <stdio.h>",
"",
"int main(int argc, const char *argv[])",
"{",
" /* code */ ",
" ", // $1 表示输入触发关键字后,按下 Tab 后光标定位到的第一个位置
"",
" return 0;",
"}",
],
"description": "main function!"
},
"mainh": {
"prefix": "mainh", // 触发的关键字 输入 mainh 按下tab键
"body": [
"#ifndef __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__",
"#define __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__",
"",
"#endif $BLOCK_COMMENT_START __${TM_FILENAME/(.*)\\..+$/${1:/upcase}/}_H__ $BLOCK_COMMENT_END",
""
],
"description": ""
},
}

3.3 settings.json

settings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[cpp]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},

4. Makefile

4.1 makefile.json

点击查看 makefile.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
// Place your snippets for makefile here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"head": {
"prefix": "head", // 触发的关键字, 输入后按下tab键
"body": [
"##============================================================================#",
"# Copyright © hk. 2022-2025. All rights reserved.",
"# File name: ${TM_FILENAME}",
"# Author : 上上签",
"# Date : $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
"# Version : ",
"# Description: ",
"##============================================================================#",
"##",
"$0",
],
"description": "Description at the beginning of file"
},
}

4.2 settings.json

settings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[makefile]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},

5. ShellScript

5.1 shellscript.json

点击查看 shellscript.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
// Place your snippets for shellscript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"head": {
"prefix": "head", // 触发的关键字 输入 head 按下tab键
"body": [
"#!/bin/bash",
"# * =====================================================",
"# * Copyright © hk. 2022-2025. All rights reserved.",
"# * File name : ${TM_FILENAME}",
"# * Author : 上上签",
"# * Date : $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
"# * ======================================================",
"##",
"",
],
"description": "Description at the beginning of file!"
},

}

4.2 settings.json

settings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"editor.quickSuggestions":{
"comments": "off",
"strings": "off",
"other": "off"
},
//用户代码片段触发
"[shellscript]":{
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},