Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sublime Text plugin to increment/decrement all selected numbers
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
@albrow

This comment has been minimized.

albrow commented Mar 5, 2013

Thanks for this! Very handy in niche situations.

@philiprenich

This comment has been minimized.

philiprenich commented Aug 2, 2013

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)

@splodingsocks

This comment has been minimized.

splodingsocks commented Aug 13, 2013

@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/

Sublime will take any class that extends one of the sublime_plugin classes
(TextCommand, WindowCommand or ApplicationCommand), remove the suffix Command and then convert the CamelCaseinto underscore_notation for the command name.

This means we add this file as a plugin, and then create a new key binding like so:

{ "keys": ["ctrl+a"], "command": "increment"},
{ "keys": ["ctrl+x"], "command": "decrement"}

I haven't tested it, but something like that should work

@joostrijneveld

This comment has been minimized.

joostrijneveld commented Mar 25, 2014

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à.

@halilim

This comment has been minimized.

halilim commented Jun 16, 2014

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
@andytill

This comment has been minimized.

andytill commented Nov 24, 2014

@dta @halilim magic

@jaimeschettini

This comment has been minimized.

jaimeschettini commented Jun 24, 2016

Thanks for this!

@biemster

This comment has been minimized.

biemster commented Sep 19, 2016

As andy said, pure magic.

@akotulu

This comment has been minimized.

akotulu commented Jan 24, 2017

Saved time, good work.

@zahirkelloud

This comment has been minimized.

zahirkelloud commented Nov 14, 2017

How can I add this to sublime text 3? and how can I use it? Thanks

@jonadem

This comment has been minimized.

jonadem commented Feb 8, 2018

@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.

@iit2011081

This comment has been minimized.

iit2011081 commented Sep 11, 2018

@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.
[
{ "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.

@cgjosephlee

This comment has been minimized.

cgjosephlee commented Oct 2, 2018

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!

@octothorpe8

This comment has been minimized.

octothorpe8 commented Nov 10, 2018

@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.
[
{ "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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment