Sublime Forum

Insert consecutive numbers without using multiple selections

#1

Hi guys

I am new to sublime text. I want to insert consecutive numbers from 1 to 50. I came across many threads that says about inserting numbers across multiple selections but in my case I am trying to insert numbers from 1 to 50 into an empty text file, hence no selection can be made.

The file should only have numbers like below. Is it possible?

1
2
3
4
5
.
.
.
50
0 Likes

#2

I think that this package can help you, but I haven’t used it.

0 Likes

#3

It says about using it on selection. I want to insert without a selection.

0 Likes

#4

an empty selection in an empty document counts as a selection :wink:

3 Likes

#5

If you use it without a selection, how will it know where to insert? I guess you could just insert at the beginning of the document… Maybe some of these suggested plugins do that if there is no selection. You may have to experiment with some of them to see.

Regardless of how it gets inserted, it may be that you desire a very specific workflow that hasn’t been written by someone yet. If that is the case, you may just need to write your own plugin.

I would go into Package Control and search for your desired functionality. You may have better luck that way than hoping for someone who happens to use the package you want to stumble on this thread, or for someone to do the research for you. If you can’t find what you need, I’d take a look at writing your own plugin. People are generally helpful here and can answer plugin coding questions. EDIT: it is possible you’ve already researched as much as you can; don’t want to sound like I’m accusing you of not doing research yourself.

If you describe in detail the functionality you are hoping to achieve, as the details of exactly how you expect the plugin to work are still a little vague, maybe someone will throw together a simple example as what you want most likey would be pretty trivial to throw together.

2 Likes

#6

There are undoubtedly many packages available that could be made to do what you want to do, although if you’re new to Sublime that might seem a little overwhelming. As has been mentioned, a better idea of what you’re ultimately trying to accomplish may make it easier for someone to come up with something more concrete that’s not too heavy-weight.

By way of a simple example that inserts a sequence of numbers into the current file, there is this:

import sublime
import sublime_plugin

class InsertNumbersCommand(sublime_plugin.TextCommand):
    def input(self, first, second):
        if second is None:
            return self.view.window().show_input_panel("To", "50",
                       lambda val: self.input(first, val), None, None)

        for x in range(int(first), int(second) + 1):
            self.view.run_command("insert", {"characters": "%d\n" % x})

    def run(self, edit):
        self.view.window().show_input_panel("From", "1",
            lambda val: self.input(val, None), None, None)

This implements a simple command named insert_numbers which will prompt you (in an input box near the bottom of the window) for a number to start from and a number to go to, and will then insert that sequence of numbers into the current file one after the other on their own lines.

It’s simple so it doesn’t do error checking like making sure that you enter actual numbers, for example. It uses the insert command to insert the numbers, so if you have multiple selections it will insert the sequence at all of them. overwrite the current selection with the sequence, etc.

To play with it you can select Tools > Developer > New Plugin... from the menu, then replace the contents of the template plugin with this text and save it in the location that Sublime defaults to (your User package). From there you can bind insert_numbers to a key to actually run it.

2 Likes

#7

Thanks for the responses. I think, I didn’t explain it properly. If I want to insert numbers from 1 to 5000 into a file, I would open an excel workbook. Insert 1 in the first column,then select the cell and drag it down 5000 lines and choose “Fill series”. Then I would copy the complete numbers from 1 to 5000 from the excel into a new file in sublime text. I want to know, if there is a way to generate these numbers in sublime text with out having to use excel.

0 Likes

#8

@anishjp read what @OdatNurd has posted. Your answer is in there.

0 Likes

#9

This is also probably not what you want to do, but if you don’t want to make a selection or write a package, then with “line_numbers” = “true” in the settings file, all you have to do is press Enter 49 times. Simple! :slight_smile:

0 Likes

#10

@OdatNurd: Great, your code does just what I was looking for. Really appreciate your help, thank you.[quote=“OdatNurd, post:6, topic:30177, full:true”]
There are undoubtedly many packages available that could be made to do what you want to do, although if you’re new to Sublime that might seem a little overwhelming. As has been mentioned, a better idea of what you’re ultimately trying to accomplish may make it easier for someone to come up with something more concrete that’s not too heavy-weight.

By way of a simple example that inserts a sequence of numbers into the current file, there is this:

import sublime
import sublime_plugin

class InsertNumbersCommand(sublime_plugin.TextCommand):
    def input(self, first, second):
        if second is None:
            return self.view.window().show_input_panel("To", "50",
                       lambda val: self.input(first, val), None, None)

        for x in range(int(first), int(second) + 1):
            self.view.run_command("insert", {"characters": "%d\n" % x})

    def run(self, edit):
        self.view.window().show_input_panel("From", "1",
            lambda val: self.input(val, None), None, None)

This implements a simple command named insert_numbers which will prompt you (in an input box near the bottom of the window) for a number to start from and a number to go to, and will then insert that sequence of numbers into the current file one after the other on their own lines.

It’s simple so it doesn’t do error checking like making sure that you enter actual numbers, for example. It uses the insert command to insert the numbers, so if you have multiple selections it will insert the sequence at all of them. overwrite the current selection with the sequence, etc.

To play with it you can select Tools > Developer > New Plugin... from the menu, then replace the contents of the template plugin with this text and save it in the location that Sublime defaults to (your User package). From there you can bind insert_numbers to a key to actually run it.
[/quote]

2 Likes

#11

You would achieve the mentioned functionality with the InsertNums expression @p==50 (the package linked by @Jackeroo earlier).

3 Likes

#12

Yes, that works too, thanks very much.

0 Likes