使用vscode Snippets实现快速添加文件头信息

使用过pycharm的用户应该知道,可以使用模板自动在文件开头添加文件信息。
使用vscode Snippets实现快速添加文件头信息_第1张图片


作为vscode爱好者,有没有可能在vscode上实现这个功能呢?答案肯定是能,而且要比pycharm更灵活!
但这里的实现机制有所区别,pycharm是自动添加,而vscode需要输入关键字触发,这个功能叫做Snippets,官方解释是:

Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements.

翻译过来就是为了提高重复代码输入的效率,所以说比pycharm更为灵活!!!
在python中使用的前提是要安装python扩展(做python开发的应该都装了)
使用vscode Snippets实现快速添加文件头信息_第2张图片
File >> Preferences >> User Snippets
使用vscode Snippets实现快速添加文件头信息_第3张图片
选择python
使用vscode Snippets实现快速添加文件头信息_第4张图片
在打开的python.json中修改代码:

{
	// Place your snippets for python 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"
	// }
	"Add file info in the beginning": {
		"prefix": "fileinfo",
		"body": [
			"#!/usr/bin/env python3",
			"# -*- coding: utf-8 -*-",
			"# @author : microfat",
			"# @time   : ${CURRENT_MONTH}/${CURRENT_DATE}/${CURRENT_YEAR_SHORT} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
			"# @File   : ${TM_FILENAME}"
		],
		"description": "Add file header"
	}
}

其中@author换成你的名字,然后保存

实现效果:
使用vscode Snippets实现快速添加文件头信息_第5张图片
更多Snippets玩法可以参考官方文档:
https://code.visualstudio.com/docs/editor/userdefinedsnippets

你可能感兴趣的:(Python,工具,vscode,python,pycharm)