由于编写的Python文件基本都有中文,所以每次创建文件都要手动添加

# -*- coding: UTF-8 -*-

所以特别麻烦,这里记录一下如何在Vs Code中设置自动添加代码片段,解放这个无意义的重复性工作

进入用户代码片段

image.png

选择Python

image.png

输入自定义信息

{
	"HEADER": {
		"prefix": "header",
		"body": [
			"#!/usr/bin/env python",
			"# -*- encoding: utf-8 -*-",
			"'''",
			"@File    :   $TM_FILENAME",
			"@Time    :   $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
			"@Author  :   村长",
			"@Version :   1.0",
			"@Contact :   faith01238@hotmail.com",
			"@Desc    :   当前文件作用",
			"'''",
			"",
			"",
			"$0"
		],
	}
}

请根据自身信息修改

创建效果

触发代码片段

手动在第一行输入

header

image.png

然后根据程序提示选中并回车即可

最终效果

image.png

扩展

同时针对个人需求,也可以添加自定义内容,例如

{
	"HEADER": {
		"prefix": "header",
		"body": [
			"#!/usr/bin/env python",
			"# -*- encoding: utf-8 -*-",
			"'''",
			"@File    :   $TM_FILENAME",
			"@Time    :   $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
			"@Author  :   村长",
			"@Version :   1.0",
			"@Contact :   faith01238@hotmail.com",
			"@Homepage : https://liumou.site'",
			"@Desc    :   当前文件作用",
			"'''",
			"",
			"",
			"$0"
		],
	}
}