22

Is there a way to have multiple "commands" associated with one shortcut?

I have these two shortcuts. First shortcut makes the window on the left larger than the right one (in a 2 column view) and the next shortcut puts the focus on the first window. I tend to forget one or the other shortcut when coding quickly.

{
    "keys": ["super+alt+left"],
    "command": "set_layout",
    "args":
    {
        "cols": [0.0, 0.66, 1.0],
        "rows": [0.0, 1.0],
        "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
    }
},
{ "keys": ["ctrl+alt+left"], "command": "focus_group", "args": { "group": 0 } }

This question makes me sound like i'm lazy but i'd like to think of it as being efficient.

Any advice or suggestions, please?

John Mellor
  • 12,572
  • 4
  • 46
  • 35
Satans Sidekick
  • 221
  • 2
  • 4
  • 1
    Funny how I found this question, wanting to do the exact same sequence of commands. – David Pärsson May 05 '13 at 14:14
  • **Note: (added 2023-02-17)** Some content in this thread may reference stale links to the SublimeText docs. Readers may experience better results by replacing **BEFORE** [http://docs.sublimetext.info/en/latest](http://docs.sublimetext.info/en/latest) with **AFTER** [https://docs.sublimetext.io/guide](https://docs.sublimetext.io/guide). Also known as: "Sublime Text Community Documentation" – dreftymac Feb 17 '23 at 15:10

3 Answers3

37

As of Sublime Text 4 (build 4104, 3 May 2021) you can use the built in chain command.

In older versions (ST2 or ST3) you needed to install the Chain of Command plugin (GitHub).

Both have the same syntax, allowing you write keybindings etc that perform multiple actions, e.g.:

{ "keys": ["ctrl+d"],
  "context": [
    { "key": "panel_visible", "operator": "equal", "operand": true }
  ],
  "command": "chain",
  "args": {
    "commands": [
      ["hide_panel", {"cancel": true}],
      ["find_under_expand"]
    ]
  }
},

which redefines Ctrl+D so that it'll close the Find panel if it's open, then perform its normal action (Quick Add Next).

You can do any number of subcommands. Each is an array with the command name (e.g. "hide_panel") followed optionally by the arguments (e.g. {"cancel": true}). The unofficial/incomplete documentation of available commands and their arguments may be helpful.

John Mellor
  • 12,572
  • 4
  • 46
  • 35
  • 1
    This plugin deserve more stars – user2081518 Jul 05 '16 at 10:44
  • Awesome plugin, this should be marked as the answer – Long M K Nguyễn Mar 16 '18 at 23:46
  • **Note:** For anyone just learning sublime, you can also attach multiple commands to a custom class in a plugin you write yourself, such as `class MyCustomCommand(sublime_plugin.TextCommand)` which you can write in python. See [your-first-plugin](http://docs.sublimetext.info/en/latest/extensibility/plugins.html?highlight=custom%20plugin#your-first-plugin) – dreftymac Sep 20 '19 at 00:38
14

There's a post on the Sublime Text 2 forum that includes code for a generic "run multiple commands" plugin. It will allow you to bind multiple commands to any key binding the same way you'd normally bind them to one:

  {
    "keys": ["super+alt+left"],
    "command": "run_multiple_commands",
    "args": {
      "commands": [
        { "command": "set_layout", "args": { "cols": [0.0, 0.66, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]] } },
        { "command": "focus_group", "args": { "group": 0 } }
      ]
    }
  }

Note that this is untested, and you must install the plugin provided in the post for this to work.

Alternatively, you can create a plugin for a specific set of commands following the instructions in this answer.

Community
  • 1
  • 1
Sara
  • 8,222
  • 1
  • 37
  • 52
  • 1
    For these very commands, `"context": "window"` must be added as well. For example: `{ "command": "focus_group", "args": { "group": 0 }, "context": "window" }`. – David Pärsson May 05 '13 at 14:13
8

You can record a macro (using the Tools menu), then save it and set a keyboard shortcut to call it using

{"keys": ["super+alt+l"], "command": "run_macro_file", "args": {"file": "res://Packages/User/Example.sublime-macro"}}

http://docs.sublimetext.info/en/latest/extensibility/macros.html

Granted, this isn't quite what you're asking for, but may provide the same end for others with similar questions.

Jon
  • 1,189
  • 11
  • 17