详解pycharm新建文件时头部的模板

PyCharm头部模板定制

我的博客已全部迁往个人博客站点:oukohou.wang,敬请前往~~



pycharm新建文件时的头部模板,即默认的: author = ‘$USER’,pycharm下依次“File→settings→Editor→File and Code Template”即可找到界面,如下:
pycharm模板设置界面

如何对pycharm头部模板进行个人的全新定制呢?

JetBrains的官方帮助文档里说的很详细了,这里画蛇添足赘述一下,努力做到详细精确,傻瓜操作,即copy即用:

pycharm总共支持四种方法来创建模板:
1. Creating a file template from scratch
2. Creating a file template from an existing one
3. Creating a file template from a file
4. Creating and referencing include templates

1、Creating a file template from scratch

这是最简单常用的一种方法,以python为例,模板的默认头部为:

__author__ = '$USER'
# '$USER' 为预定义的变量,为你当前登录的计算机用户名

其他的一些可选的预定义变量有:

${PROJECT_NAME} - the name of the current project.
${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
${USER} - the login name of the current user.
${DATE} - the current system date.
${TIME} - the current system time.
${YEAR} - the current year.
${MONTH} - the current month.
${DAY} - the current day of the month.
${HOUR} - the current hour.
${MINUTE} - the current minute.
${PRODUCT_NAME} - the name of the IDE in which the file will be created.
${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.

例如,如果你还想加上该文件创建的时间:

__time__   = '${DATE}'

当然,也可以组合使用:

__time__   = '${DATE} ${TIME}'

#显示如下:
__author__ = 'oukohou'
__time__ = '2017/3/14 10:46'

而如果你觉得预定义的这些太少啦,不能满足你狂拽酷炫的需求,那么,pycharm也支持自定义,格式为:

#set( $GirlFriend = "Jingjing" )
__girlFriend__ = '${GirlFriend}'

显示如下:
__girlFriend__ = 'Jingjing'

还可以覆盖预定义变量:

#set($USER = "Handsome")
__author__ = '$USER'

#显示如下:
__author__ = 'Handsome'

美元符号本身用’${DS}’,示例:

__dollar__ = '${DS}'

#显示如下:
__dollar__ = '$'

**2、Creating a file template from an existing one **

就是copy已存在的头文件模板,示意图如下:
copyTemplate1

先选中一个要copy的模板,然后点击复制,效果如下:

copyTemplate2

3、Creating a file template from a file

这个是将一个文件保存为模板,示意图如下:

saveFileasTemplate1
这时再次按照“File→settings→Editor→File and Code Template”打开,就会发现刚刚添加的模板了。

4、Creating and referencing include templates

这里用到了Template选项旁边的include选项:

parseTemplate1

点击“+”号创建,写入内容,然后再返回Template选项通过“parse”来引用,这里依然以python的模板为例:

parseTemplate2

显示如下:

# -*- coding: utf-8 -*-
__author__ = 'Handsome'
__time__ = '2017/3/14 14:28'
__girlFriend__ = 'Jingjing'
__dollar__ = '$'

hello, Jingjing.
# If this runs wrong, don't ask me, I don't know why;
# If this runs right, thank god, and I don't know why.
# Maybe the answer, my friend, is blowing in the wind.

以上。
你的赞是我的动力。

个人github链接:oukohou的git ,后续会迁移一些东西,暂时比较空。
个人微信公共号:oukohou
hello there.
Scan and we will see.

see also: JetBrains官方帮助文档Apache-Velocity

PyCharm 中设置创建文件自动生成文件头注释,可以通过配置 **File and Code Templates** 来实现。以下是详细的设置方法: ### 配置文件头注释模板 1. 打开 PyCharm,点击菜单栏的 `File`,然后选择 `Settings`(在 macOS 上为 `Preferences`)。 2. 在设置窗口中,依次选择 `Editor` → `File and Code Templates`。 3. 在右侧的模板列表中找到 `Python Script`,点击进入编辑界面。 4. 在模板编辑区域输入你希望自动生成的文件头注释内容。例如: ```python #!/usr/bin/env python # -*- coding: UTF-8 -*- ''' @Project :${PROJECT_NAME} @File :${NAME}.py @IDE :${PRODUCT_NAME} @Author :${USER} @Date :${DATE} ${TIME} @Description: 请在此处添加文件功能描述 ''' ``` 该模板使用了 PyCharm 提供的预定义变量,如 `${PROJECT_NAME}` 表示项目名称,`${NAME}` 表示文件名,`${DATE}` 和 `${TIME}` 分别表示当前日期和间[^2]。 ### 可用变量说明 PyCharm 提供了一系列预定义变量,用于动态插入信息到模板中,包括: - `${PROJECT_NAME}`:项目名称。 - `${PRODUCT_NAME}`:IDE 名称(如 PyCharm)。 - `${NAME}`:新建文件的名称。 - `${USER}`:当前操作系统的用户名。 - `${DATE}`:当前日期(格式为年-月-日)。 - `${TIME}`:当前间(格式为:分:秒)。 - `${YEAR}`:当前年份。 - `${MONTH}`:当前月份(两位数字)。 - `${MONTH_NAME_SHORT}`:当前月份的短名称(如 7月)。 - `${MONTH_NAME_FULL}`:当前月份的全称(如 七月)。 - `${DAY}`:当月的第几天。 - `${HOUR}`:当前小。 - `${MINUTE}`:当前分钟。 - `${SECOND}`:当前秒数[^2]。 ### 应用模板并查看效果 完成模板编辑后,点击 `Apply` 或 `OK` 以保存更改。之后,每次新建 `.py` 文件PyCharm 将自动应用你设置的模板,并填充相应的变量内容。 例如,新建一个名为 `example.py` 的文件,生成的文件头注释可能如下所示: ```python #!/usr/bin/env python # -*- coding: UTF-8 -*- ''' @Project :MyProject @File :example.py @IDE :PyCharm @Author :admin @Date :2025-04-05 14:30:00 @Description: 示例文件,用于演示自动生成的文件头注释 ''' ``` ### 其他注意事项 - 模板内容可以根据个人或团队需求进行自定义,确保统一性和规范性。 - 如果需要调整光标位置(例如,希望新建文件光标自动跳转到特定行),可以通过修改代码模板配合 Live Templates 实现[^3]。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值