sublime text 自定义插件,自动插入署名,自定义插入日期,自动生成头部注释

本文介绍如何在Sublime Text中创建和使用代码片段(snippet),实现自动插入日期、作者等信息,并通过自定义快捷键提高编程效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动插入署名

菜单下面的

一、工具(tool)>新代码段(new snippet…)

看到以下代码

<snippet>
    <content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

1.snippet介绍

content:其中必须包含<![CDATA[…]]>,否则无法工作, Type your snippet here用来写你自己的代码片段
tabTrigger:用来引发代码片段的字符或者字符串, 比如在以上例子上, 在编辑窗口输入hello然后按下tab就会在编辑器输出Type your snippet here这段代码片段
scope: 表示你的代码片段会在那种语言环境下激活, 比如上面代码定义了source.python, 意思是这段代码片段会在python语言环境下激活.
description :展示代码片段的描述, 如果不写的话, 默认使用代码片段的文件名作为描述

2.Snippet可以存储在任何的文件夹中, 并且以.sublime-snippet为文件扩展名, 默认是存储在.sublime-snippet文件夹下。

二、将以下代码粘贴到文件中

<snippet>
    <content><![CDATA[
/**
 * date: ${1:ctrl+alt+shift+d}
 * author: ${2:}
 * descride:${3:}
 */
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>headmark</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

保存的文件后缀是.sublime-snippet 

在文件里面输入headmark,按tab就可以出来了

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

快速插入日期

菜单下面的

一、工具(tool)>新代码段(new plugin…)

看到以下代码

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

将以下代码粘贴进去

import datetime, getpass
import sublime, sublime_plugin
class AddDateTimeStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } )
class AddDateStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%Y-%m-%d") } )
class AddTimeStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%H:%M:%S") } )

保存的文件后缀是.py 

二、自定义快捷键

preferences>键绑定-用户

[
    {"keys": ["ctrl+alt+shift+d"], "command": "add_date_time_stamp" },
    {"keys": ["ctrl+alt+d"], "command": "add_date_stamp" },
    {"keys": ["ctrl+alt+t"], "command": "add_time_stamp" }
]

保存就可以了使用了

转载于:https://www.cnblogs.com/wangzhaobo/p/8862473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值