VSCode设置新建文本模板

  之前写Python使用的是PyCharm作为开发环境的,虽然软件很好用,自带各种Python常用功能和代码补全,奈何其正版太贵了,和谐版会有各种麻烦,所以最终尝试了VSCode这个宇宙第一编辑器٩(ˊᗜˋ*)و ,发现果然很好用,用了一段时间后发现也能和PyCharm一样在新建文本时自动套用模板。

设置代码片段

  VSCode设置新建文本模板很简单,通过插入用户代码片段就可以完成:

  • 操作路径如下 文件 -> 首选项 -> 用户代码片段,然后在弹框中选择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"
    }
}
- "Print to console":表示代码片段名称;
- "prefix":表示呼出代码片段时的快捷方式;
- "body":代码块内容;换行使用\r\n;
- "description":说明内容,输入快捷方式时VSCode显示的内容;
- "$1,$2,$0":指定代码模块生成后,编辑光标出现位置; 使用Tab键进行切换(编辑光标按$1,$2,$3...$0的顺序跳转),$0是光标最后可切换位置。
  • 使用如下配置直接覆盖文档内所有配置或者按照自己需求修改配置模板,修改Author和email即可,我自己使用的是GPL3.0协议,你可以换成自己的开源协议(如何使用GPL协议:https://www.gnu.org/licenses/gpl-howto.html),注意修改GPL协议上面Copyright中的Author
{
    "Python header template": {
        "prefix": "py_file_init",
        "body": [
            "#!/usr/bin/python3",
            "# -*- coding:utf-8 -*-",
            "'''",
            "@File    :   $TM_FILENAME",
            "@Time    :   $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
            "@Author  :   Author",
            "@Version :   1.0",
            "@Contact :   email",
            "@Desc    :   None$1",
            "@License :",
            "",
            "    (c) Copyright 2020 Author",
            "",
            "    This program is free software: you can redistribute it and/or modify",
            "    it under the terms of the GNU General Public License as published by",
            "    the Free Software Foundation, either version 3 of the License, or(at your option) any later version.",
            "",
            "    This program is distributed in the hope that it will be useful,",
            "    but WITHOUT ANY WARRANTY;without even the implied warranty of",
            "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the",
            "    GNU General Public License",
            "    for more details.",
            "",
            "    You should have received a copy of the GNU General Public License",
            "    along with this program.If not, see < https: //www.gnu.org/licenses/>.",
            "'''",
            "",
            "# here put the import lib",
            "$0",
            "",
            "",
            "",
            "",
            "if __name__ == '__main__':",
            "",
        ],
        "description": "Python header template"
    }
}
  • 常用变量:
    • TM_FILENAME 当前被编辑文档名
    • TM_FILENAME_BASE 当前被编辑文档名,没有后缀
    • TM_DIRECTORY 当前被编辑文档目录
    • TM_FILEPATH 当前被编辑文档全路径
    • CLIPBOARD 当前剪切板内容
    • TM_SELECTED_TEXT 当前选中内容或空字符串
    • TM_CURRENT_LINE 当前行内容
    • TM_CURRENT_WORD 光标处字符或空字符串
    • TM_LINE_INDEX 从0开始的行号
    • TM_LINE_NUMBER 从1开始的行号
  • 日期时间变量:
    • 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 秒数

使用代码片段

  在新建的.py文档中直接键入py_file_init就可以看到刚才建立的代码片段(实际上当你输入py时VSCode可能就已经自动识别到了),直接回车即可插入模板,光标首先会在@Desc项目后停留,方便插入项目描述,再次按下Tab键后会移动到"# here put the import lib"下,方便插入库文件
vscode_snippets_154eaf6f6b2000570.jpg

参考链接:https://juejin.im/post/5d52646f51882541082a8834
VSCode中文文档:https://jeasonstudio.gitbooks.io/vscode-cn-doc/content/md/定制化/用户定义代码段.html

Enjoy it!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值