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

前端 未结 3 908
情话喂你
情话喂你 2020-12-07 07:01

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          


        
3条回答
  •  没有蜡笔的小新
    2020-12-07 07:36

    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

提交回复
热议问题