Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Sublime Text2 | Pluginの作成

概要

Sublime Text2のPluginの作成について。

内容

Sublime Text2のPlugin開発はPythonで行います。

手順

・Tools,New Pluginを押下
・テンプレートコードを変更
Pythonで処理内容を実装

サンプルコード 日付の入力機能
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import datetime
import locale
 
class GetSystemDateCommand(sublime_plugin.TextCommand):
  def run(self, edit, **kwargs):
    if kwargs == None:
      kwargs["format"] = "yyyymmdd"

    date = datetime.datetime.today()
    ret = ""

    format = kwargs["format"]
    if format == "yyyy/mm/dd hh:mi:ss":
      ret =  '%04d/%02d/%02d %02d:%02d:%02d' % (date.year, date.month, date.day, date.hour, date.minute, date.second)
    elif format == "yyyy/mm/dd":
      ret = '%04d/%02d/%02d' % (date.year, date.month, date.day)
    elif format == "yyyymmdd":
      ret = '%04d%02d%02d' % (date.year, date.month, date.day)
    else:
      return ""

    return self.view.insert(edit, self.view.sel()[0].a, ret)
ショートカットキーの設定
{ "keys": ["ctrl+alt+d", "1"], "command": "get_system_date", "args": {"format": "yyyy/mm/dd hh:mi:ss"} },
{ "keys": ["ctrl+alt+d", "2"], "command": "get_system_date", "args": {"format": "yyyy/mm/dd"} },
{ "keys": ["ctrl+alt+d", "3"], "command": "get_system_date", "args": {"format": "yyyymmdd"} }
コンソールから実行する場合

・ctrl + `
・下記コマンドを入力
view.run_command('get_system_date', {"format": "yyyymmdd"})
view.run_command('get_system_date', {"format": "yyyy/mm/dd})
view.run_command('get_system_date', {"format": "yyyy/mm/dd hh:mi:ss"})
※get_system_date部がコマンド名

Pythonメモ

・日付の利用

import datetime
import locale

・キー付き可変長引数

def run(self, edit, **kwargs):

・クラス名の調査

instance.__class__