LoginSignup
3

More than 5 years have passed since last update.

PythonでSublime Text3のタブを検索できるようにするプラグインを作成する

Last updated at Posted at 2016-12-20

Sublime Text3で自分はタブを大量に開くのですが、皆さんはどのように大量のタブの中から対象のタブを探しているのでしょうか?
色々やり方はあると思うのですが、多くは以下になるかなと思います。

  • ctrl + tab(control + tab)で移動して探す
  • Alt + 数字(Command + 数字)で番号でジャンプする
  • そもそもタブを大量に開かない。。。

このうちだと、ctrl + tab で探すのもめんどくさいし、 Alt + 数字 でジャンプするのは10個以上タブ開いてるし、そもそも番号わからない。。。
3つ目はちょっと。。。

ということで、勉強がてらタブを検索する機能を作ってみました。
ちなみにPython初心者です。

そもそも、Sublimeにタブを検索する機能あるんですかね?

前提

  • Windowsです
  • Sublime Text3を日本語化してます

プラグイン作成準備

[ツール] => [プラグイン追加] をクリック。
ひな形が作成されるので、 ctrl + s で保存する。

保存する場所として Packages/User が選択されていることを確認して、適当な名前を拡張子 .py で保存する

ソースコードを書く

以下のソースコードのみでタブ検索を実現できます。
すっごい簡単

import sublime, sublime_plugin
import os

class FindTabListCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        def on_done(index):
            if index == -1:
                return;
            window = sublime.active_window()
            tabs = window.views_in_group(window.active_group())
            window.focus_view(tabs[index])

        window = sublime.active_window()
        tabs = window.views_in_group(window.active_group())
        tabNames = []
        for item in tabs:
            if item.name() != "Find Results":
                fileName = os.path.basename(item.file_name())
                tabNames.append(fileName)

        window.show_quick_panel(tabNames, on_done)

ショートカットを設定する

キーバインドの設定で以下のように設定すれば完了です。

{ "keys": ["ctrl+t"], "command": "find_tab_list"}

実行するとSublimeではおなじみの以下のような検索ボックスが開いてタブを検索することが出来ます。

2016-12-20_20h47_23.png

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
3