Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Sublime Text 2 plugin to enable running multiple commands in any given context from a single key binding.
[
{
"keys": ["ctrl+w"],
"command": "run_multiple",
"args": {
"commands": [
{"command": "find_under_expand", "args": null, "context": "window"},
{"command": "show_panel", "args": {"panel": "find"}, "context": "window"}
]
}
}
]
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)
@celsomiranda

This comment has been minimized.

celsomiranda commented Feb 8, 2013

This was really helpful.
Thanks!

@RaphaelDDL

This comment has been minimized.

RaphaelDDL commented Jul 17, 2013

Awesome gist.
Thank you very much for sharing.

@ryanrobertsname

This comment has been minimized.

ryanrobertsname commented Nov 23, 2013

Works great, very helpful. Thanks!

@elgalu

This comment has been minimized.

elgalu commented Jan 16, 2014

Excellent!! Even works in ST3, just tested on Build 3059 :)

@varaskkar

This comment has been minimized.

varaskkar commented Feb 18, 2014

Works incredibly on ST3, thanks!

@joseadrian

This comment has been minimized.

joseadrian commented Jul 30, 2014

Thank you so much!

@ctf0

This comment has been minimized.

ctf0 commented Sep 5, 2014

it possible to add a scope to which syntax the command should run at ?

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