-
-
Save dtao/2788978 to your computer and use it in GitHub Desktop.
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 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à.
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
Thanks for this!
As andy said, pure magic.
Saved time, good work.
How can I add this to sublime text 3? and how can I use it? Thanks
@zahirkelloud
Place the file under the Packages directory (For example:
C:\Users<Username>\AppData\Roaming\Sublime Text 3\Packages\User ).
In ST3: Preferences > Key Bindings
Add the snippet given above with your chosen binding.
Restart ST3 and use the binding.
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.
[
{ "keys": ["ctrl+alt+up"], "command": "increment" },
{ "keys": ["ctrl+alt+down"], "command": "decrement"}
]
But nothing is happening when I am using the ctrl+alt+up/down command. Do I need to give the file path in key binding file.
Thank you for the awesome script! Really helps.
I have created a package for easy use, hope you like it!
https://github.com/cgjosephlee/increment_decrement
Now available on package control channel!
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.
[
{ "keys": ["ctrl+alt+up"], "command": "increment" },
{ "keys": ["ctrl+alt+down"], "command": "decrement"}
]
But nothing is happening when I am using the ctrl+alt+up/down command. Do I need to give the file path in key binding file.
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...
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...
@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