ctrl+P:通过查找跳转到目录下任意文件;
ctrl+shift+P:通过查找使用快捷键;
ctrl+M:跳转到匹配的括号处;
ctrl+G:跳转到行;
alt+数字:跳转到对应位置的标签页;
ctrl+PgUp/PgDn:按顺序切换标签页;
ctrl+F/H:查找/替换;
在查找下的快捷键:
alt+R:正则表达式开关;
alt+C:大小写开关;
alt+W:精确匹配开关;
enter:查找下一个;
shift+enter:查找上一个;
alt+enter:查找全部;
ctrl+R:查找函数;
ctrl+;:查找;
ctrl+X:删除行;
ctrl+enter:行后插入行;
ctrl+shift+enter:行前插入行;
ctrl+L:选中行;
ctrl+shitf+J:合并行;
ctrl+shift+/:块注释;
ctrl+shift+V:粘贴并自动缩进;
鼠标右键+Shift
添加到选择:鼠标右键+Shift+Ctrl
从选择中删除:鼠标右键+Shift+Alt
snippet:文件后缀.sublime-snippet的xml文件。
若想要在
<content>标签中写$应该写\$。
<snippet>
<content><![CDATA[<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial scale=1">
<title>${2:Untitled}</title>
</head>
<body>
Hello ${3:$TM_FULLNAME}! Welcome to $2!
$0
</body>
</html>]]>
</content>
<tabTrigger>doctype</tabTrigger>
<description>HTML5 Structure</description>
<scope>text.html</scope>
</snippet>
tabTrigger:触发snippet的关键字;
scope:表示此snippet的作用范围(文件类型),scope的完整类型见https://gist.github.com/danpe/6993237;Hello you are sb!
触发此snippet之后,首先光标会到达content="$1"的$1处;
然后会到${2:Unititled}处,此处的默认值为Untitled;
接着会到${3:$TM_FULLNAME}处,此处的$TM_FULLNAME为环境变量,对于完整的环境变量见https://gist.github.com/danpe/6996806;
最后退出snippet时,光标会到$0处,若没有$0,则默认跳转到snippet的最后。
在sublime官网文档可以找到所有
command命令。
command命令可以使用控制台进行调用:
1. ctrl+`打开控制台
2. 输入<object>.run_command(<command>, { <command_param1>: <command_arg1>, ...})
其中
1.<object>可以是sublime、window或view,分别代表当前sublime应用对象、当前sublime窗口对象、和当前的标签页对象,分别对应三种不同的command类型:ApplicationCommand、WindowCommand、TextCommand
2.<command>表示command的名称,<command_param1>表示command的参数名,<command_arg1>表示command的参数值。
例如,在当前标签页文件的末尾插入一段字符串,可以使用
view.run_command('append', {'characters': "插入的文字", 'scroll_to_end': False})
根据sublime官网文档,
append命令的格式为:
appendInserts a string at the end of the view.
- characters (String): String to be inserted.
- force (Bool):
- scroll_to_end (Bool):
另外也可以用命令面板进行调用:
1. ctrl+shift+P打开命令面板
2. 查找需要的命令并用鼠标单击/按下enter键
注
有些command会根据当前标签页的情况判断是否展示,比如一个应用于C语言的command会检查文件的后缀名是否为
.c。
略
| command类型 | 对应的对象 | 表示的实体 |
|---|---|---|
ApplicationCommand |
sublime对象 |
sublime进程 |
WindowCommand |
window对象 |
一个sublime应用窗口 |
TextCommand |
view对象 |
sublime窗口中的一个标签页 |
WindowCommand实例拥有.window属性,表示当前窗口。
TextCommand实例拥有.view属性,表示当前标签页。
三种类型都拥有:
- .run()方法:执行该command;
- is_visible方法:是否在Command Palette(即ctrl+shift+P出现的哪个)中显示;
insert用于简单地往光标处插入字符串:
view.run_command('insert', {'characters': '插入的文字'})
insert_snippet可以插入一段snippet(这意味着可以使用$1等的占位符):
view.run_command('insert_snippet', { 'contents': 'hello ${1: sb}! $0'})
也可以直接调用一个snippet文件:
view.run_command('insert_snippet', { 'name': 'Packages/User/test.sublime-snippet'})
插件其实就是用户写一个属于自己的command。
插件使用python语言编写。
Tools/Developer/New Plugin,出现一个新的标签页,里面会默认有一个示例的.py文件。import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
ctrl+s保存到默认的路径,可以随意命名。这样一个简单的plugin就创建好了。
运行创建好的plugin:
- ctrl+`打开控制台,输入view.run_command('example')。
可以看到打开标签页的开头被插入了"Hello, World"。
plugin必须是名字为XxxCommand的python类
注
(而且必须继承
sublime_plugin.<object>Command,<object>为Text、Window或Application,参见3.2 command的三种类型)。
sublime会将XxxCommand去掉后面的Command并将剩余的部分转化为蛇形命名形式(例如OpenFileCommand ==> open_file)作为command的名字。
import datetime
import sublime_plugin
class AddCurrentTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command('insert',
{'characters': "%s" % datetime.datetime.now().strftime("%y/%m/%d/%H:%M")}
)
调用了insert这个command进行插入。
import datetime
import sublime_plugin
class AddCurrentTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
# 定义要插入的字符串
time_str = "%s" % datetime.datetime.now().strftime("%y/%m/%d/%H:%M")
# 获取当前选择(光标)的区域
sel = self.view.sel()
# 遍历所有的选区(通常只有一个光标位置)
for region in sel:
# 在光标位置插入文本
self.view.insert(edit, region.begin(), time_str)
view表示当前的标签页,view.sel()会返回一组region(Region类型表示光标选中的文本区域),region.begin()会返回选中区域的开始位置(类型为Point,表示以文件开头为起始的偏移量),接着用view.insert()方法,指定位置并插入文本。