sublime text 给选中项插入编号

 1 #coding=utf-8
 2 import datetime, getpass
 3 import sublime, sublime_plugin
 4 import re
 5 
 6 # 插数字
 7 class InsertNumberCommand(sublime_plugin.TextCommand):
 8     def run(self, edit):
 9         self.view.window().show_input_panel("input start_num and diff_num:", "start_num:1, diff_num:1", lambda text: self.accumulate(text, edit), None, None)
10     def accumulate(self, text, edit):
11         text =  re.sub(r"[^\d\+-]*([\+-]?\d+)[,\s\t]+[^\d\+-]*([\+-]?\d+)", r"\1 \2", text)
12         numbers = text.split(" ")
13         start_num   = int(numbers[0])
14         diff_num    = int(numbers[1])
15         for region in self.view.sel():
16             #(row,col) = self.view.rowcol(region.begin())
17             self.view.insert(edit, region.end(), "%d" %start_num)
18             start_num += diff_num

 

 1 #coding=utf-8
 2 import datetime, getpass
 3 import sublime, sublime_plugin
 4 
 5 # 求和
 6 class SumCommand(sublime_plugin.TextCommand):
 7     def run(self, edit):
 8         sum_all = 0
 9         for region in self.view.sel():
10             add = 0
11             str_region = self.view.substr(region)
12             try:
13                 add = int(str_region)
14             except ValueError:
15                 sublime.error_message(u"含有非数字的字符串")
16                 return
17             sum_all = sum_all + add
18 
19         sublime.message_dialog(str(sum_all))
20 
21 class SelectWordCommand(sublime_plugin.TextCommand):
22     def run(self, edit):
23         for region in self.view.sel():
24             reg = self.view.word(region)
25             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

 

转载于:https://www.cnblogs.com/hangj/p/3954175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值