LoginSignup
26
27

More than 5 years have passed since last update.

SublimeText3用、マルチカーソルに連番の数字を入力するプラグイン

Posted at

SublimeTextをバージョン3にアップデートしたところ、「Sublime Text 2でマルチカーソルに連番の数字を入力するプラグイン」が動作しなくなったため、ST3対応版を作ってみました。

プラグインの入力欄に「1」を入力しEnterすると、上のカーソルから順に1, 2, 3, 4,...と入力し、
「2 5 3」と入力すると00002, 00005, 00008, 00011,...と入力されます。

multi_number_input.py
import sublime, sublime_plugin

class MultiNumberInputCommand(sublime_plugin.TextCommand):
    def on_done(self, arg):
        paramsArray = arg.split(" ")
        paramsArray.extend(['', '']) #minimum length is 3
        initial     = int(paramsArray[0]) if paramsArray[0] != '' else 1
        digits      = int(paramsArray[1]) if paramsArray[1] != '' else 1
        interval    = int(paramsArray[2]) if paramsArray[2] != '' else 1
        self.view.run_command('insert_numbers', {'args': [initial, interval, digits]})

    def run(self, edit):
            self.view.window().show_input_panel('Initial Digits Step | ex) 2 5 3: ', '', self.on_done, None, None)


class InsertNumbers(sublime_plugin.TextCommand):
  def run(self, edit, args):
    number = args[0]
    interval = args[1]
    digits = args[2]

    for region in self.view.sel():
        #for negative values
        if number < 0:
            my_digits = digits + 1
        else:
            my_digits = digits

        formatter = '{0:0' + str(my_digits) + 'd}'
        formatted = formatter.format(number)
        self.view.replace(edit, region, str(formatted))
        number = number + interval

オリジナル版からの変更点は

  • ST3に対応した
  • 未入力時は「1」として処理するようになった
  • " by " ではなく単一スペースでパラメータを与えるようになった
  • 桁数指定機能を実装した

桁数指定方法は「初期値 桁数 ステップ」としてください。例)

  • 「2」 → 2, 3, 4, 5,...
  • 「2 5」 → 00002, 00003, 00004, 00005,...
  • 「2  3」 → 2, 5, 8, 11,...
  • 「2 5 3」 → 00002, 00005, 00008, 00011,...

PackageControllerからは追加できないので、手動でインストールしてください。
正直Pythonははじめてなので、変なところありましたらご教示いただけると幸いです。

26
27
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
27