Sublime Forum

HowTo: Insert continuing numbers

#1

Hi folks,

can someone please tell me how to insert continuing numbers?

for example i have those lines: (note: the ‘x’ are placeholders only, there may be nothing at that place)

CMDx=
CMDx=
CMDx=

How can i insert continuing numbers?
CMD1=
CMD2=
CMD3=
or
CMD001=
CMD002=
CMD003=


And then, is it possible increase an number in an block?

F.ex. i have:
CMDx=
Parax=
Iconx=

CMDx=
Parax=
Iconx=

and i want to get:
CMD1=
Para1=
Icon1=

CMD2=
Para2=
Icon2=


or i may have:
CMD1=
Para1=
Icon1=

and i want to copy this block multible times and increase the counter each time.

Thanks for any suggestion :wink:

0 Likes

#2

There is no built-in function to do what you want, you must use a plugin.
There is one example for inserting continuing numbers in this post http://www.sublimetext.com/forum/viewtopic.php?f=5&t=1604&start=0.
The code is short and rather easy to understand, so start from there and customize it for your need.

Good luck.

0 Likes

#3

Wonderful, thank you!

InsertNum works at least for to Insert continuing numbers, but not with padding.
I can also insert line numbers in front of the text, even starting with numbers relative to the current line.

If somebody have some spare time to implement “pad with n zeros”… would be fine, i am not onto python yet :wink:
I imagine an syntax to enter “001 1” and get “001,002,003,…”. Anyone?

BTW, just for the record, I also play around with inc-dec-value plugin…

0 Likes

#4

I editted it a bit and came up with this:

import sublime, sublime_plugin

class PromptIncrementNumberWithPaddingCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel('Enter a starting number, step and zero padding.', '1, 1, 1', self.on_done, None, None)
        pass

    def on_done(self, text):
        try:
            (current, step, padding) = map(str, text.split(","))
            if self.window.active_view():
                self.window.active_view().run_command("increment_number_with_padding", {"current" : current, "step" : step, "padding" : padding} )
        except ValueError:
            pass

class IncrementNumberWithPaddingCommand(sublime_plugin.TextCommand):

    def run(self, edit, current, step, padding):
    # def run(self, edit, current, step):
        sublime.status_message("Inserting #")
        current = int(current)
        for region in self.view.sel():
            sublime.status_message("Inserting #" + str(current))
            self.view.replace(edit, region, str('%0*d' %(int(padding), int(current))))
            current = current + int(step)

It will ask you for starting number, step and width it should fit.

If you specify 1, 1, 1 the first number is 1.
If you specify 1, 1, 5, the first number is 00001.

0 Likes

#5

Works crazyarm , many thanks.

And the class name from
"class PromptIncrementNumberWithPaddingCommand()"

without “command” and the words separated by underscore

is the trigger to use in the keymap like
{ “keys”: “ctrl+alt+n”], “command”: “prompt_increment_number_with_padding” } ?

Was not that obvious for me as newbie.

That python still not looks familiar to me (VBS+JS) but looks like elegant coding.
(current, step, padding) = map(str, text.split(",")) looks very nice.
Is there an python howto especially for SublimeText snippets?
EDIT: ok, i try first => net.tutsplus.com/tutorials/pytho … -2-plugin/

Thanks for all the help.

0 Likes