198

Is there a way to add insert a number that is incremented once per cursor in Sublime Text 2?

Example, with | as the cursor:

Lorem ipsum dolor sit amet, |
vehicula sed, mauris nam eget| 
neque a pede nullam, ducimus adipiscing, 
vestibulum pellentesque pellentesque laoreet faucibus.|

Desired result:

Lorem ipsum dolor sit amet, 1|
vehicula sed, mauris nam eget2| 
neque a pede nullam, ducimus adipiscing, 
vestibulum pellentesque pellentesque laoreet faucibus.3|

Does this functionality exist natively, or is there a plugin providing it?

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130

3 Answers3

334

I recommend the plugin Text Pastry. The Number Sequence command is the one you need.

I prefer to use the Insert Nums command:

Text Pastry has a build in support for the Insert Nums syntax by providing three numbers separated by one space:

N M P

N: the start index.

M represents the step size which will be added to the index for each selection.

P must be > 0 and will be used to pad the index with leading zeroes.

PJSCopeland
  • 2,818
  • 1
  • 26
  • 40
aanton
  • 5,572
  • 1
  • 23
  • 15
  • 64
    Ridiculously helpful. – digitalextremist Oct 10 '13 at 03:03
  • 1
    Text Pastry : Multi-select :: Multi-select : Find & Replace – gfullam May 12 '14 at 19:22
  • This plugin seems perfect for me. But I need to increment from 1 to 5 and repeat multiple times. How would I go about doing this? – Chucky Apr 02 '15 at 11:16
  • 4
    I found that I had to first select the lines and use Ctrl + Shift + L to select all affected lines, then use Text Pastry for numbering, https://www.youtube.com/watch?v=upEieoTwnjs – Manish Jul 04 '15 at 13:04
  • make a multi line selection (not only cursors) with the start number selected. Press ctrl + alt + T, it opens a nice menu which explaines the rest. – pscheit Oct 27 '15 at 07:00
  • 15
    This solution works in Sublime Text 3 as well. For me the primary issue was that you need to know how to open the Text Pastry command line (CTRL-ALT-N). So, to get incrementing numbers from 01 to 10, select 10 lines (shift+right-click/drag the desired columns), press `CTRL-ALT-N`, then type `1 1 2` – Gus Feb 04 '16 at 20:48
  • Chucky: https://github.com/duydao/Text-Pastry/wiki/Number-Range explains how to do what you want. TL;DR: `range [ [ ]]` – PJSCopeland May 12 '16 at 21:41
110

I think that the only way to achieve what you ask is to create your own plugin.

Tools/New Plugin...:

import sublime_plugin


class IncrementSelectionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        start_value = int(self.view.substr(self.view.sel()[0]))

        counter = 0
        for selection in self.view.sel():
            self.view.insert(edit, selection.begin(), str(start_value + counter))
            counter = counter + 1

        for selection in self.view.sel():
            self.view.erase(edit, selection)

Save it in your User directory. Then add a shortcut to your Key Bindings - User:

{ "keys": ["YOUR_SHORTCUT"], "command": "increment_selection" }

Now you can place the cursors where you need:

enter image description here

Insert the number the counter should start from (in this case 1):

enter image description here

Select the number you typed (shift<—):

enter image description here

Type the shortcut:

enter image description here

Riccardo Marotti
  • 20,218
  • 7
  • 70
  • 53
  • 5
    Great answer, very helpful. I will do this as soon as I get a chance. – Michael Robinson Jan 29 '13 at 19:44
  • I've attempted to make this plugin, but haven't been able to get it to work - placing multiple cursors, inserting `1` and pressing my hotkey does nothing. Is there something I need to to in order to activate new plugin? – Michael Robinson Jan 29 '13 at 20:40
  • Before pressing your hot keys, you have to select the text you entered, with shift+left arrow (look at the second to last screenshot in my text). – Riccardo Marotti Jan 29 '13 at 21:13
  • 1
    Yes I did do that. My hotkey is: ` { "keys": ["ctrl+alt+i"], "command": "increment_selection" }`, in Key Bindings - User. No output in ST2 console – Michael Robinson Jan 29 '13 at 22:57
  • 1
    I verified the code both on Mac and Windows, and it should work. Try to look at the console after you have typed the shortcut: maybe there is some error that could help to understand the problem (`View/Show Console`). – Riccardo Marotti Jan 30 '13 at 08:00
  • I'm using Ubuntu, and I did look @ the console, nothing. I will have another go in the morning. – Michael Robinson Jan 30 '13 at 08:47
  • In the meanwhile, I verified it on Ubuntu too, and it worked. I'm sorry, I don't know what can be wrong... Try to add a `print("something")` in the `run` method, to verify that the plugin is invoked by the shortcut. – Riccardo Marotti Jan 30 '13 at 09:04
  • I'm sure this does work but the plugin `Text Pastry` mentioned in a subsequent answer provides a solution that directly solves my original question - a plugin that provides this functionality. – Michael Robinson Feb 03 '13 at 09:21
  • Yes, `Text Pastry` is definitely a better solution! – Riccardo Marotti Feb 03 '13 at 09:34
  • I successfully used this today, though when i placed '1' as my selection number it failed with ' ValueError: invalid literal for int() with base 10: '' '. Works fine when entering 0 though. Thanks!! – Icementhols Jan 25 '14 at 08:46
  • This should be mark as Answer. Excellent!! Thanks Working in SL3 too. – Nono Feb 20 '14 at 10:51
  • great idea as usage / ui for solving this problem – pscheit Jun 07 '15 at 07:10
  • This is so very handy and useful. Just took me seconds to have it inside my ecosystem. Thanks a lot, buddy. – softvar Aug 07 '17 at 20:01
  • 1
    Great plugin! The only downside is that every integer in the selection needs to be of the exact same value. – Gabe Hiemstra May 02 '19 at 08:40
24

You want to had a number at each row that you have selected, but not the same. For exemple, you select 5 cursors and you want to write 1 2 3 4 5.

select your 5 cursors enter image description here maybe you can use ctrl + maj + L on the highlighted lines

ctrl + maj + P and select arithmetic enter image description here

Because you have 5 cursors, it propose 1 2 3 4 5
enter image description here enter image description here

If you want you can change your number of iteration
enter image description here

Or start from an other number than 1
enter image description here

Add odd number
enter image description here

Nicoolasens
  • 2,871
  • 17
  • 22
  • 3
    This should be accepted answer. As it appears that Sublime supports this natively and no extra plugin is needed. – DelphyM Jun 02 '22 at 22:08