sublime text 给选中项插入编号
#coding=utf-8
import datetime, getpass
import sublime, sublime_plugin
import re # 插数字
class InsertNumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel("input start_num and step:", "start:1, step:1", lambda text: self.accumulate(text, edit), None, None)
def accumulate(self, text, edit):
text = re.sub(r"[^\d\+-]*([\+-]?\d+)[,\s\t]+[^\d\+-]*([\+-]?\d+)", r"\1 \2", text)
numbers = text.split(" ")
start_num = int(numbers[0])
diff_num = int(numbers[1])
for region in self.view.sel():
#(row,col) = self.view.rowcol(region.begin())
self.view.insert(edit, region.end(), "%d" %start_num)
start_num += diff_num
Sublime Text 3 版本运行会报错 ValueError: Edit objects may not be used after the TextCommand's run method has returned ,需要把 callback 改成一个独立的 command
import sublime
import sublime_plugin
import datetime, getpass
import re # 插数字
class InsertNumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel("input start_num and step:", "start:1, step:1", lambda text: self.view.run_command('insert_number_cb', {"text": text}), None, None) class InsertNumberCbCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
# sublime.message_dialog(text)
text = re.sub(r"[^\d\+-]*([\+-]?\d+)[,\s\t]+[^\d\+-]*([\+-]?\d+)", r"\1 \2", text)
numbers = text.split(" ")
start_num = int(numbers[0])
diff_num = int(numbers[1])
for region in self.view.sel():
#(row,col) = self.view.rowcol(region.begin())
self.view.insert(edit, region.end(), "%d" %start_num)
start_num += diff_num
求和:
#coding=utf-8
import datetime, getpass
import sublime, sublime_plugin # 求和
class SumCommand(sublime_plugin.TextCommand):
def run(self, edit):
sum_all = 0
for region in self.view.sel():
add = 0
str_region = self.view.substr(region)
try:
add = int(str_region)
except ValueError:
sublime.error_message(u"含有非数字的字符串")
return
sum_all = sum_all + add sublime.message_dialog(str(sum_all)) class SelectWordCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
reg = self.view.word(region)
self.view.sel().add(reg)
写配置文件时,可以根据数据的规律,编写适当的函数。根据 count 计算对应的数据。


测试:
ctrl + ` 打开 command window
输入 view.run_command('insert_number') 回车
http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/extensibility/plugins.html
步骤:
Tools -> New Plugin...
粘贴以上代码,两份代码可以放在同一个文件里
command + s 保存文件为 insertNumber.py 文件名可以随便取~
快捷键设置:
Sublime Text 2 -> Preferences -> Key Bindings - User
如果打开的文件为空,可以参考 Key Bindings - Default 文件中的格式,添加一条新的配置
{ "keys": ["super+shift+f5"], "command": "insert_number" }
"insert_number" 对应类名 InsertNumberCommand
sublime text 给选中项插入编号的更多相关文章
- sublime text 自定义插件,自动插入署名,自定义插入日期,自动生成头部注释
自动插入署名 菜单下面的 一.工具(tool)>新代码段(new snippet…) 看到以下代码 <snippet> <content><![CDATA[ Hel ...
- Sublime Text 3 文本编辑器
1.安装下载 下载地址:http://www.cr173.com/soft/121149.html http://www.xiazaiba.com/html/24343.html 官网 http:// ...
- 编辑器sublime text 加入到右键菜单
方式一: 1. 运行中输入 regedit 打开注册表 2. 在HKEY_CLASSES_ROOT/*/shell/ 下新建’项’ ,名称自己觉得.我用的是Sublime Text 3 ...
- 鼠标右键添加Sublime Text
鼠标右键添加Sublime Text 参考 将sublime添加到鼠标右键 实践 1. win+R 输入regedit 2. 输入路径: 计算机\HKEY_CLASSES_ROOT\*\shell\ ...
- 将Sublime Text 添加到鼠标右键菜单的教程方法
安装notepad++软件,在菜单右键自动会添加“edit with notepad++"的选项,那么怎么将Sublime Text 添加到鼠标右键菜单呢?下面是我的操作过程,希望有帮助! ...
- jquery获取select选中项的文本
使用jquery获取选中的值很简单 $("#select").val(); 但是获取选中的文本就没有这么直接了 $("#select").find(" ...
- sublime text按esc经常进入command mode(不能输入任何东西)
在使用sublime text进行 选中 操作中,如果使用了esc退出选中状态,会进入command mode,现象是不能输入任何东西,关闭当前编辑文件重新打开可以解决.但是很影响连贯性.可以通过一些 ...
- Win10系统右键添加Sublime Text 3的打开方式
1.打开注册表编辑器,开始->运行->regedit. 2.在HKEY_CLASSSES_ROOT→ * → Shell 下,在Shell下,新建项命名为Open With Sublime ...
- Sublime Text自定制代码片段(Code Snippets)
在编写代码的整个过程中,开发人员经常会一次又一次的改写或者重用相同的代码段,消除这种重复过程的方法之一是把我们经常用到的代码保存成代码片段(snippets),这使得我们可以方便的检索和使用它们. 为 ...
随机推荐
- [PHP] 转义字符 Escape character
\n is a symbol for new line \t is a symbol for tab and \r is for 'return'
- qstring转string
Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 1 QString str("死亡使者赛维"); 这样直接用带中文的字符串进行构造,那么用Q ...
- Laravel 5.5 Api
Laravel api token验证使用方法 从 Laravel 5.2 开始, Laravel 的将路由的配置进行了分拆, 在 routes 目录下有 web.php 和 api.php 两个路由 ...
- tty linux 打开和设置范例
http://bbs.csdn.net/topics/340184140 /************************************************************** ...
- 2018.10.15 bzoj4445: [Scoi2015]小凸想跑步(半平面交)
传送门 话说去年的省选计算几何难度跟前几年比起来根本不能做啊(虽然去年考的时候并没有学过计算几何) 这题就是推个式子然后上半平面交就做完了. 什么? 怎么推式子? 先把题目的概率转换成求出可行区域. ...
- Scrapy学习篇(十三)之scrapy-splash
之前我们学习的内容都是抓取静态页面,每次请求,它的网页全部信息将会一次呈现出来. 但是,像比如一些购物网站,他们的商品信息都是js加载出来的,并且会有ajax异步加载.像这样的情况,直接使用scrap ...
- 破解Excel密码
https://zhidao.baidu.com/question/98055974.html 方法:1\打开文件2\工具---宏----录制新宏---输入名字如:aa3\停止录制(这样得到一个空宏) ...
- Codeforces777A Shell Game 2017-05-04 17:11 59人阅读 评论(0) 收藏
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- [smarty] 在smarty模板中使用smarty变量初始化 javascript 变量的问题
// 总结:// 1/ 在smarty 模板文件中,使用从php中assign过来的smarty变量,一定需要使用双引号或单引号来括住smarty变量,如:var title="<!- ...
- matlab中使用正弦波合成方波(带动画)
x=:*pi; :: s=; ::step s = s+/i*sin(i*x); end plot(s);set(figure(),'visible','off'); filename=[num2st ...