import sublime | |
import sublime_plugin | |
class NumberCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
selection = self.view.sel() | |
for region in selection: | |
try: | |
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 |
This comment has been minimized.
This comment has been minimized.
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) |
This comment has been minimized.
This comment has been minimized.
@philliprenich, 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 |
This comment has been minimized.
This comment has been minimized.
I can confirm that what @murphyrandle suggested does indeed work. TL;DR on creating a plugin: create a folder in your packages directory, place this file in it and add the key bindings listed above to your keybindings file. Voilà. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for this! |
This comment has been minimized.
This comment has been minimized.
As andy said, pure magic. |
This comment has been minimized.
This comment has been minimized.
Saved time, good work. |
This comment has been minimized.
This comment has been minimized.
How can I add this to sublime text 3? and how can I use it? Thanks |
This comment has been minimized.
This comment has been minimized.
@zahirkelloud In ST3: Preferences > Key Bindings Restart ST3 and use the binding. |
This comment has been minimized.
This comment has been minimized.
@jonadem @splodingsocks 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. |
This comment has been minimized.
This comment has been minimized.
Thank you for the awesome script! Really helps. |
This comment has been minimized.
This comment has been minimized.
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... |
This comment has been minimized.
This comment has been minimized.
Hey buddy, thanks for this code, i am a beginner kindly please tell me how i can use this code... |
This comment has been minimized.
Thanks for this! Very handy in niche situations.