Skip to content

Instantly share code, notes, and snippets.

@FichteFoll
Last active December 28, 2015 10:19
Show Gist options
  • Save FichteFoll/7485244 to your computer and use it in GitHub Desktop.
Save FichteFoll/7485244 to your computer and use it in GitHub Desktop.
Command for running macro-like commands; can also run window commands (indicated by preceding "_") and repeat commands. Uses sublime_lib's WindowAndTextCommand (https://github.com/SublimeText/AAAPackageDev/blob/master/Lib/sublime_lib/__init__.py)
import sublime_lib
# See https://github.com/SublimeText/AAAPackageDev/blob/master/Lib/sublime_lib/__init__.py
# Example usage:
# // ; before ) or } or "
# { "keys": [";"], "command": "batch", "args": {
# "commands": [
# ["move", {"by": "characters", "forward": true}],
# ["insert", {"characters": ";"}]
# ]},
# "context": [
# { "key": "following_text", "operator": "regex_contains", "operand": "^[\\})\"](?!;)"}
# ]
# },
# // Just move if ; already exists
# { "keys": [";"], "command": "batch", "args": {
# "commands": [
# ["move", {"by": "characters", "forward": true}, 2]
# ]},
# "context": [
# { "key": "following_text", "operator": "regex_contains", "operand": "^[\\})\"];"}
# ]
# },
class BatchCommand(sublime_lib.WindowAndTextCommand):
def run(self, edit=None, commands=None):
for c, *a in commands:
n = 1
print(repr(a))
if len(a) == 2:
n = a[1]
a = a[0]
# Do we actually run Application commands regularly?
print("running", c, a, n, "time(s)")
# Repeat `repeat` times
for i in range(n):
if c[:1] == "_":
self.window.run_command(c[1:], a)
else:
self.view.run_command(c, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment