在 Sublime Text 2 中为每个选择添加一个数字,每个选择递增一次

问题描述 投票:0回答:3

有没有办法在 Sublime Text 2 中添加插入每个光标递增一次的数字?

例如,以

|
为光标:

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

想要的结果:

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

此功能是原生存在的,还是有插件提供?

sublimetext2
3个回答
334
投票

我推荐插件Text Pastry数字序列命令就是您所需要的。

我更喜欢使用插入数字命令

Text Pastry 内置了对插入数字语法的支持 提供由一个空格分隔的三个数字:

N M P

N:起始索引。

M 表示将添加到索引的步长 每个选择。

P 必须 > 0 并且将用于填充索引 前导零。


110
投票

我认为实现您要求的唯一方法是创建您自己的插件。

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)

将其保存在您的

User
目录中。 然后添加一个快捷方式到您的
Key Bindings - User
:

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

现在您可以将光标放置在您需要的位置:

enter image description here

插入计数器应从的数字(在本例中为 1):

enter image description here

选择您输入的号码 (shift<—):

enter image description here

输入快捷键:

enter image description here


26
投票

您想在所选的每一行添加一个数字,但不能相同。例如,您选择 5 个光标,并且要写入 1 2 3 4 5。

选择你的5个光标也许你可以在突出显示的行上使用ctrl+maj+L

ctrl + maj + P 并选择算术

因为你有5个光标,所以建议1 2 3 4 5

如果您愿意,可以更改迭代次数

或者从1以外的数字开始

添加奇数

© www.soinside.com 2019 - 2024. All rights reserved.