Last active
November 18, 2017 12:47
-
-
Save bgmort/7ae52ea4270f1c404321c20d1b97733c to your computer and use it in GitHub Desktop.
Cleaned up version of code posted at https://forum.sublimetext.com/t/run-multiple-commands-command/6848/35. Added support for repeated commands and example key mappings to move/scroll by ten lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run_multiple_commands.py | |
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 RunMultipleCommandsCommand(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'] | |
if 'repeat' in command: | |
repeat = command['repeat'] | |
else: | |
repeat = 1 | |
# 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+'".') | |
for x in range(0, repeat): | |
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"keys": ["alt+up"], | |
"command": "run_multiple_commands", | |
"args": { | |
"commands": [ | |
{"command": "move", "args": {"by": "lines", "forward": false, "amount": 1}, "repeat": 10}, | |
] | |
} | |
}, | |
{ | |
"keys": ["alt+down"], | |
"command": "run_multiple_commands", | |
"args": { | |
"commands": [ | |
{"command": "move", "args": {"by": "lines", "forward": true, "amount": 1}, "repeat": 10}, | |
] | |
} | |
}, | |
{ | |
"keys": ["ctrl+alt+up"], | |
"command": "run_multiple_commands", | |
"args": { | |
"commands": [ | |
{"command": "scroll_lines", "args": {"amount": 10}}, | |
{"command": "move", "args": {"by": "lines", "forward": false, "amount": 1}, "repeat": 10}, | |
] | |
} | |
}, | |
{ | |
"keys": ["ctrl+alt+down"], | |
"command": "run_multiple_commands", | |
"args": { | |
"commands": [ | |
{"command": "scroll_lines", "args": {"amount": -10}}, | |
{"command": "move", "args": {"by": "lines", "forward": true, "amount": 1}, "repeat": 10}, | |
] | |
} | |
}, |
I ended up figuring out that these commands required a context (either "window", "app", "text"). If unset, it uses the view as the context which doesn't necessarily handle these sorts of commands.
Fixed below:
{
"keys": ["ctrl+c"],
"command": "run_multiple_commands",
"args":
{
"commands":
[
{
"command": "gulp_kill",
"context": "window",
"args": { "silent": false }
},
{
"command": "exec",
"context": "window",
"args": { "kill": true }
}
]
}
},
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to be working for me. I copied the py file and created it where it's supposed to be.
But my keymaps don't seem to work: (one is for cancelling gulp and the other for cancelling a build, I'd just like to run both since I only have one or the other running, never at the same time but want the same key command for both)