|def run(self, edit):|
|selection = self.view.sel()|
|for region in selection:|
|value = int(self.view.substr(region))|
|self.view.replace(edit, region, str(self.op(value)))|
|return len(self.view.sel()) > 0|
|def op(self, value):|
|return value + 1|
|def op(self, value):|
|return value - 1|
I'm a sublime plugin noob. Does this then need a keyboard bindings file to run the commands? Just having this file by itself doesn't do anything I'm guessing. Can you point me in the right direction to figure out how to add a keyboard binding to run it? (something like ctrl+up)
WBond wrote a tutorial here:
which has some info that'll help you out: http://net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/
This means we add this file as a plugin, and then create a new key binding like so:
I haven't tested it, but something like that should work
I've improved this a bit by allowing it to operate when nothing is selected. It does so by detecting number boundaries to the left and to the right of the cursor. E.g. (| = cursor position):
import sublime import sublime_plugin class NumberCommand(sublime_plugin.TextCommand): def run(self, edit): selection = self.view.sel() for region in selection: try: # Try to operate on around the cursor if nothing is selected if region.empty(): begin = region.begin() while begin >= 0: if not self.view.substr(begin - 1).isdigit(): break begin = begin - 1 end = region.end() while end < self.view.size(): if not self.view.substr(end).isdigit(): break end = end + 1 region = sublime.Region(begin, end) value = int(self.view.substr(region)) self.view.replace(edit, region, str(self.op(value))) except ValueError: pass def is_enabled(self): return len(self.view.sel()) > 0 class IncrementCommand(NumberCommand): def op(self, value): return value + 1 class DecrementCommand(NumberCommand): def op(self, value): return value - 1
I pasted this Increment_Decrement.py in my /home/Username/.config/sublime-text-3/Packages/User directory and after that I pasted following key binding commands in /home/Username/.config/sublime-text-3/Packages/User/Default (Linux).sublime-keymap.
Thank you for the awesome script! Really helps.
I have this exact question. @iit2011081 did you ever get this working?
I'm definitely a novice here, btw, just trying to add some functionality to ST3. I usually install packages (if I do) using Package Control so this is all very new to me...
Hey buddy, thanks for this code, i am a beginner kindly please tell me how i can use this code...