Created
May 25, 2012 16:10
-
-
Save dtao/2788978 to your computer and use it in GitHub Desktop.
Sublime Text plugin to increment/decrement all selected numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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):
|39px (nothing is selected -> increment: 40, 41, ..., decrement: 38, 37, ...)
39|px ("9" is selected -> increment: 310, 311, decrement: 38, 37, ...)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
Hey buddy, thanks for this code, i am a beginner kindly please tell me how i can use this code...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...