Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a number to each selection in Sublime Text 2, incremented once per selection

Tags:

sublimetext2

People also ask

How do I add numbers in Sublime Text?

Just select the text, split the selection ( ctrl+shift+l ), insert and select $ at the start of the line, and evaluate python ( ctrl+shift+x ) to get increasing numbers.

How do I change all occurrences in Sublime Text?

Alt+F3 gives a really simple way to do find and replace: Use it to select all occurrences of the current word or selection, then just start typing to replace or edit them all at once.

How do you edit multiple lines in Sublime Text?

Alternatively you can select lines and go to SELECTION MENU >> SPLIT INTO LINES. Now you can edit multiple lines, move cursors etc. for all selected lines.

How do I get multiple cursors in Sublime Text?

While you can place multiple text cursors in Sublime Text with Cmd–Click (Mac) or Ctrl–Click (Windows), here's another technique that comes in handy. Hold Ctrl–Shift (Mac) or Ctrl–Alt (Windows) and hit Up or Down Arrow to place an additional text cursor above or below the current cursor.


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.


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


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