Sublime Textはデフォルトで一つのキーバインドに一つしかコマンドを登録できません。
複数登録できるようにするには別途プラグインを追加する必要がありますので、その方法と、キーバインドの設定をまとめます。
プラグインの設定
- [Tools]>[New Plugin…]と選択
- untitledのファイルが開かれるので、下記のコードをコピペ。
- ファイルを未保存の状態で開いておく
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import sublime, sublime_plugin # Takes an array of commands (same as those you'd provide to a key binding) with # an optional context (defaults to view commands) & runs each command in order. # Valid contexts are 'text', 'window', and 'app' for running a TextCommand, # WindowCommands, or ApplicationCommand respectively. class RunMultipleCommand(sublime_plugin.TextCommand): def exec_command(self, command): if not 'command' in command: raise Exception('No command name provided.') args = None if 'args' in command: args = command['args'] # default context is the view since it's easiest to get the other contexts # from the view context = self.view if 'context' in command: context_name = command['context'] if context_name == 'window': context = context.window() elif context_name == 'app': context = sublime elif context_name == 'text': pass else: raise Exception('Invalid command context "'+context_name+'".') # skip args if not needed if args is None: context.run_command(command['command']) else: context.run_command(command['command'], args) def run(self, edit, commands = None): if commands is None: return # not an error for command in commands: self.exec_command(command) |
- パッケージのフォルダをfinderで開く([Sublime Text]>[Preferences]>[Browse Packages…])
- 1.の 〜〜/Packages/直下に”Run Multiple Commands”という名前でフォルダを作る
- untitledのファイルを名前を付けて保存。↑の/Run Multiple Commands/内に”run_multiple_commands.py”という名前で保存する(run_multiple_commands.pycも同時に作られる)
- ↑のrun_multiple_commands.pyをSublime Textで開いた状態で、Package Controlを開く[cmd+shift+P]
- “Create Package File”を入力して選択
- “Run Multiple Commands”と入力して”Default”を選択してEnter
- デスクトップに”Run Multiple Commands.sublime-package”というファイルができるのでユーザーのパッケージフォルダ(/Users/【ユーザー名】/Library/Application Support/Sublime Text 3/Packages/User)に移動
最終的に
- /Users/【ユーザー名】/Library/Application Support/Sublime Text 3/Packages/Run Multiple Commands/ にrun_multiple_commands.pyとrun_multiple_commands.pyc がある
- /Users/【ユーザー名】/Library/Application Support/Sublime Text 3/Packages/User/ にRun Multiple Commands.sublime-package がある
状態になればOK。これでプラグインの設定が完了
キーバインドの設定
ここで複数キーバインドを設定するサンプルとして、複数のファイルを編集中にしている状態で「ファイルを全て保存」した後、「ファイルを全て閉じる」という二つのコマンドを実行する設定を作ってみます。
- [Sublime Text]>[Preferences]>[Key Bindings – User]を開く
- 下記サンプルのように記述する
1 2 3 4 5 6 7 8 9 10 11 |
[ { "keys": ["alt+command+shift+s"], "command": "run_multiple", "args": { "commands": [ {"command": "save_all", "context": "window"}, {"command": "close_all", "context": "window"} ] } } ] |
- [alt] + [cmd]+ [shift] + [s] を押すと、
- “save_all”(ファイルを全て保存)した後、”close_all”(ファイルを全て閉じる)を実行する。
- commandを続けて書けば、同様にコマンドを実行できる。