Skip to content

Instantly share code, notes, and snippets.

@jugyo
Created August 13, 2012 09:54
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jugyo/3338888 to your computer and use it in GitHub Desktop.
Save jugyo/3338888 to your computer and use it in GitHub Desktop.
Sublime Text 2 Plugin Tips

Sublime Text 2 Plugin Tips

API Reference

コマンドの実行はコンソール( ctrl + ` で開ける)で以下を実行する:

view.run_command('example')

Default (OSX).sublime-keymap 等に以下を記述しておくと cmd + ctrl + e で example コマンドを実行できる:

{ "keys": ["super+ctrl+e"], "command": "example" }

先頭に "foo" を挿入する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    self.view.insert(edit, 0, "foo")

選択されている文字列を "foo" に置き換える

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    for region in self.view.sel():
        if not region.empty():
            self.view.replace(edit, region, 'foo')

複数箇所が選択されているケースがあるので for でループしてますね。

選択されている文字列を削除する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    for region in self.view.sel():
        if not region.empty():
            self.view.erase(edit, region)

選択されている文字列を取得する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            print self.view.substr(region)

選択されている箇所の行全体を取得する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    for region in self.view.sel():
        region_of_line = self.view.line(region)
        print self.view.substr(region_of_line)

full_line を使うと改行も含めて取得できる。

カーソル位置の単語を取得する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    for region in self.view.sel():
        region_of_word = self.view.word(region)
        print self.view.substr(region_of_word)

ファイルの中のすべての "foo" を選択する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        self.view.sel().clear()
        for region in view.find_all('foo'):
            view.sel().add(region)

ステータスバーに "foo" を表示する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sublime.status_message("foo")

あまり目立たないですね。

コンソールに "foo" を表示する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print("foo")

デバッグ用に使えるかな。

Default プラグインで view = self.view とやっているのをよく見かけたので真似した。

オブジェクト定義されている名前を列挙する

コンソールで dir 関数を使って調べる

dir(view)

とか

dir(sublime)

確認ダイアログを出す

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if sublime.ok_cancel_dialog("OK?", "OK"):
            print "OK"
        else:
            print "Cancel"

"foo" をダイアログで表示する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sublime.message_dialog("foo")

エラーメッセージをダイアログで表示する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sublime.error_message("error!")

クリップボードから文字列を取得する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print sublime.get_clipboard()

クリップボードに "foo" をセットする

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sublime.set_clipboard("foo")

設定されている値を取得する

'default_line_ending' という設定値を取得してみる:

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit, **args):
    default_line_ending = self.view.settings().get('default_line_ending')
    sublime.message_dialog(default_line_ending)

プラグイン固有の設定ファイルを使う

Example.sublime-settings というファイルを用意し:

{
  "foo": "bar"
}

以下のようにして利用できる:

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  SETTINGS = sublime.load_settings("Example.sublime-settings")
  def run(self, edit, **args):
    sublime.message_dialog(self.SETTINGS.get("foo"))

設定ファイルから設定を取得して変更して保存する

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings('Preferences.sublime-settings')
        settings.set('color_scheme', 'Packages/Color Scheme - Default/Espresso Libre.tmTheme')
        sublime.save_settings('Preferences.sublime-settings')

コマンドに引数を渡す

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit, **args):
    for k, v in args.iteritems():
      sublime.message_dialog("%s: %s" % (k, v))

XXX.sublime-keymap で以下のようにしてショートカットキーと結びつけることができる:

[
  {"keys": ["super+e"], "command": "example", "args": {"title": "foo", "message": "bar"}}
]

コンソール等から以下のようにして呼び出すことができる:

view.run_command('example', {"title": "foo", "message": "bar"})

インプットパネルを出す

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.WindowCommand):
  def run(self):
    # show_input_panel(caption, initial_text, on_done, on_change, on_cancel)
    self.window.show_input_panel('caption', '', self.on_done, None, None)

  def on_done(self, result):
    sublime.message_dialog(result)

sublime_plugin.WindowCommand クラスを継承してコマンドを作る。 sublime_plugin.TextCommand とは run メソッドの引数の数が違う。 on_done というコールバックメソッドで結果を取得している。

追記: 後で気づいたことだけど、TextCommand でも self.view.window() として window を取得できるので、別に WindowCommand である必要はない

何かが起きた時に何かする

sublime_plugin.EventListener を継承したクラスを作るらしい。後で調べる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment