Skip to content

Instantly share code, notes, and snippets.

@dtao
Created May 25, 2012 16:10
Show Gist options
  • Save dtao/2788978 to your computer and use it in GitHub Desktop.
Save dtao/2788978 to your computer and use it in GitHub Desktop.
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
@philiprenich
Copy link

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)

@mrmurphy
Copy link

@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
Copy link

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
Copy link

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
Copy link

@dta @halilim magic

@jaimeschettini
Copy link

Thanks for this!

@biemster
Copy link

As andy said, pure magic.

@akotulu
Copy link

akotulu commented Jan 24, 2017

Saved time, good work.

@zahirkelloud
Copy link

zahirkelloud commented Nov 14, 2017

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

@jonadem
Copy link

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
Copy link

@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
Copy link

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
Copy link

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

@dev-gnsairam
Copy link

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