前言

我之前一直想写一个记录自己笔记的软件,可以给因为我都记录在桌面的便签上很乱,以至于便签上满满的全是字母,很难看,但是我有不想写图形化界面,所以最终我选择了写一个sublime 插件,功能就是记录和查询我找到的一些api,以及linux一些命令,和一些代码片段(sublime text 3带有代码片段功能,不过书写格式较为复制,我只喜欢复制粘贴然后改几个变量,有记录和查询功能就可以了)。

开始编程

首先要了解一些基本的知识,为了后面写插件理解

  1. sublime 自带一个python,这个pythonsublime里面集成的,因为sublime插件都是python脚本,你可以通过 ctrl +` 来进入这种交互,在sublime中叫 console(控制台)
  2. 基于前一点,我们可以知道,如果写的插件想要调用自己安装的第三方库是不可能的,因为编程使用的pythonsublime自带的python完全是两个环境,就连版本可能都不一样
  3. 调试插件不能再使用你系统中安装的 python ,必须使用 sublime自带的python来调试,其实这些都是基于第一点,怎么调试后面会讲到

打开sublime--------tools-----developer-----new plugin

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

这段代码不能使用 ctrl+b执行,首先要保存,保存在默认目录里面就可以,名字随便起
那怎么执行这个插件呢?
两种办法:

  1. 打开 console(ctrl+`),在底部输入 sublime.active_window().run_command('example')回车出现,Hello,World!

  2. ctrl+shirft+p输入 key bindings回车,进入快捷键配置,配置一个快捷键

{ "keys": ["ctrl+t"], "command": "example" }

其实类的名字除去Command就是这里面命令的名字,跟文件名字没有关系
保存后,ctrl+t 就执行这个插件了

其实这个插件的作用就是在你编辑的位置插入 Hello world!
关于具体里面的api 可以参考官方文档
http://www.sublimetext.com/docs/3/api_reference.html

插件示例

import sublime
import sublime_plugin
import sys
import os
import threading
import time class InputCommand(sublime_plugin.TextCommand):
def run(self, edit): # 初始化
path = os.path.join(os.environ['HOME'],'notes')
if not os.path.exists(path):
os.makedirs(path)
self.path = path
self.code_file_path = os.path.join(self.path,'code.txt')
self.api_file_path = os.path.join(self.path,'api.txt')
self.linux_file_path = os.path.join(self.path,'linux.txt')
if not os.path.exists(self.code_file_path):
f = open(self.code_file_path,'w')
f.close()
if not os.path.exists(self.api_file_path):
f = open(self.api_file_path,'w')
f.close()
if not os.path.exists(self.linux_file_path):
f = open(self.linux_file_path,'w')
f.close() list_data = [
"new code snippets",
"new api snippets",
"new linux command",
"search code snippets",
"search api snippets",
"search linux command",
] self.fun_list = [
'new_code',
'new_api',
'new_linux',
'search_code',
'search_api',
'search_linux'
] sublime.active_window().show_quick_panel(list_data, self.on_run) def on_run(self,index):
if index != -1:
threading.Thread(target=eval('self.'+self.fun_list[index])).start() def new_code(self):
sublime.active_window().open_file(self.code_file_path)
# sublime.Window.open_file("hello",sublime.TRANSIENT) def new_api(self):
def on_start(args):
self.save(args+"\n",self.api_file_path)
sublime.active_window().show_input_panel('输入api(中文说明和命令间用#分割):','',on_start, None,None) def new_linux(self):
def on_start(args):
self.save(args+"\n",self.linux_file_path)
sublime.active_window().show_input_panel('输入linux命令(中文说明和命令间用#分割):','',on_start, None,None) def search_code(self):
data_list = self.read(self.code_file_path).split('####\n')
self.data = data_list
sublime.active_window().show_quick_panel([[i.split('\n')[0][1:],i[i.find("\n")+1:]] for i in data_list if i], self.on_select) def search_api(self):
data_list = self.read(self.api_file_path).split('\n')
self.data = [i.split('#')[1] for i in data_list if i]
sublime.active_window().show_quick_panel([i.split('#') for i in data_list if i], self.on_select) def search_linux(self):
data_list = self.read(self.linux_file_path).split('\n')
self.data = [i.split('#')[1] for i in data_list if i]
sublime.active_window().show_quick_panel([i.split('#') for i in data_list if i], self.on_select) def read(self,filepath):
f = open(filepath,'r')
data = f.read()
f.close()
return data def save(self,args,filepath):
with open(filepath,'a') as f:
f.write(args)
sublime.Window.status_message(sublime.active_window(),"已保存") def on_select(self,index):
sublime.set_clipboard(self.data[index])
sublime.Window.status_message(sublime.active_window(),"已复制到剪切板") # self.view.insert(edit, 0, "fuck this World!")

这个插件就是我前面说的我自己记录笔记的插件,其实就是简单的调用一些sublime的api,以及对文本进行读和写

最新文章

  1. javascript中对象在OOP方面的一些知识(主要是prototype和__proto__相关)
  2. du 使用详解 linux查看目录大小 linux统计目录大小并排序 查看目录下所有一级子目录文件夹大小 du -h --max-depth=1 |grep [
  3. python 读写文件和设置文件的字符编码
  4. Js笔试题之正则表达式
  5. Ubuntu16.04+hadoop2.7.3环境搭建
  6. hdu 1250 Hat's Fibonacci
  7. Sales Order Flow Statuses
  8. iOS面试知识集锦
  9. 在virtual pc中搭建基于ubuntu 的git环境
  10. file的name值
  11. 使用Html5的DeviceOrientation特性实现摇一摇功能
  12. 利用Java提供的Observer接口和Observable类实现观察者模式
  13. 系统学习java高并发系列三
  14. go get golang.org被墙问题解决
  15. QQ邮箱开启SMTP方法如何授权
  16. Django Cookie,Session
  17. 王之泰201771010131《面向对象程序设计(java)》第十七周学习总结
  18. 绑定本地的Session
  19. 马凯军201771010116《面向对象程序设计(java)》第二周学习总结
  20. Spring 4 官方文档学习 Web MVC 框架

热门文章

  1. Redis变慢?深入浅出Redis性能诊断系列文章(一)
  2. C语言001--hello world编译详解
  3. int和String的相互转化
  4. 讲讲 tcp_tw_recycle,tcp_tw_reuse
  5. 7.第六篇 二进制安装 kube-apiserver
  6. Centos7主机安装Cockpit管理其他主机
  7. 在 CentOS8/RHEL8 中配置 Rsyslog 服务器
  8. Handler机制与生产者消费者模式
  9. 高可用(vrrp)以及mysql主主备份部署
  10. PAT (Basic Level) Practice 1028 人口普查 分数 20