Jamal的博客

sublime:sublime-snipper设置自定义文件头信息

有时候写md,例如写文档的时候,希望加上文件头信息,例如:

1
2
3
4
5
6
7
---
title: sublime: sublime-snipper设置自定义文件头信息.md
date: 2017-12-02 23:08:26
tags:
- sublime
categories: sublime
---

希望加上这样一个头信息,每次添加太麻烦,可以在sublime自定义snipper自定义生成。
Tools->Developer->New Snipper
新建文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<snippet>
<content><![CDATA[
---
title: $TM_FILENAME
date: ${1:ctrl+alt+shift+d}
tags:
-
categories:
---
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>author</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>

保存在固定位置,然后新建文件,拷贝如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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") } )

保存在固定位置,名字随意。
最后在perperence->key binding->user,添加以下信息:

1
2
3
{"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" }

重启后新建文件,在文件随意位置输入author,然后点tab,就可以生成文件头,按ctrl+alt+shift+d,就出现了时间