Sublime Forum

Multiple command with custom delay for each


I want to find a way to open a text file and go to specific text.
If you run several command at once, they do not always work.
And it needed a delay to work correctly…

Found this topic make some change to work with st3
Run Multiple Commands.. Command

But it doesn’t work it give 1 general delay and than run all next command at once.

What can be done to fix it?

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']

    # 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':
        raise Exception('Invalid command context "'+context_name+'".')

    if 'delay' in command:
      delay = command['delay']
      delay = 0

    # skip args if not needed
    if args is None:
      sublime.set_timeout(lambda: sublime.active_window().run_command(command['command']),delay)
      sublime.set_timeout(lambda: sublime.active_window().run_command(command['command'], args),delay)

  def run(self, edit, commands = None):
    if commands is None:
      return # not an error
    for command in commands:
      # sublime.set_timeout(lambda: self.perform_action(view),2500)



“Open a file” is async and there is no guarantee that a file will be opened in X seconds. So I think you are on a wrong track.

Write a dedicated plugin for your use case is the only thing I figured out.

1 Like