【问题标题】:Add a number to each selection in Sublime Text 2, incremented once per selection在 Sublime Text 2 中为每个选择添加一个数字,每个选择增加一次
【发布时间】:2013-01-12 13:16:47
【问题描述】:

有没有办法在 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


    【解决方案1】:

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

    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" }
    

    现在您可以将光标放在您需要的地方:

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

    选择您输入的数字(shift):

    输入快捷方式:

    【讨论】:

    • 很好的答案,很有帮助。我一有机会就会这样做。
    • 我试图制作这个插件,但无法让它工作 - 放置多个光标,插入 1 并按下我的热键什么都不做。为了激活新插件,我需要做些什么吗?
    • 是的,我确实这样做了。我的热键是:` { "keys": ["ctrl+alt+i"], "command": "increment_selection" }`,在 Key Bindings - User 中。 ST2 控制台无输出
    • 我在 Mac 和 Windows 上都验证了代码,它应该可以工作。输入快捷方式后尝试查看控制台:可能有一些错误有助于理解问题(View/Show Console)。
    • 很棒的插件!唯一的缺点是选择中的每个整数都需要具有完全相同的值。
    【解决方案2】:

    我推荐插件Text PastryNumber Sequence command 是您需要的。

    我更喜欢使用Insert Nums command

    Text Pastry 内置了对 Insert Nums 语法的支持 提供三个用一个空格分隔的数字:

    N M P

    N:起始索引。

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

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

    【讨论】:

    • 非常有帮助。
    • 文本糕点:多选 :: 多选:查找和替换
    • 这个插件对我来说似乎很完美。但我需要从 1 增加到 5 并重复多次。我该怎么做呢?
    • 我发现我必须先选择行并使用 Ctrl + Shift + L 选择所有受影响的行,然后使用 Text Pastry 进行编号,youtube.com/watch?v=upEieoTwnjs
    • 此解决方案也适用于 Sublime Text 3。对我来说,主要问题是您需要知道如何打开 Text Pastry 命令行 (CTRL-ALT-N)。因此,要获得从 01 到 10 的递增数字,请选择 10 行(shift+右键单击/拖动所需的列),按CTRL-ALT-N,然后键入1 1 2
    【解决方案3】:

    您希望在您选择的每一行都有一个数字,但不一样。例如,你选择了 5 个光标,你想写 1 2 3 4 5。

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

    ctrl + maj + P 并选择算术

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

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

    或者从 1 以外的其他数字开始

    添加奇数

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2017-11-30
      • 1970-01-01
      相关资源
      最近更新 更多