A common use of plugins is to add extra commands to the vocabulary of Cloud9. Commands consist of code to execute a name and key binding. Commands can be referenced by menu items and buttons as way to trigger the command. Commands are always found in the commands pane and they key bindings can be configured in the key bindings editor in the preferences.

The following example shows how to create a very basic command.

commands.addCommand({
    name: "oneplusone",
    exec: function(){ 
        console.log(1 + 1);
    },
}, plugin);

The command is called oneplusone and when triggered the exec function is called, printing 2 in the browser's console. Here's a short code snippet that triggers this command:

commands.exec("oneplusone");

The next example is more detailed and specifies a lot more details about the command. This specific example is used in the format json tutorial;

commands.addCommand({
    name: "formatjson",
    group: "Format",
    bindKey: { 
        mac: "Shift-Command-J", 
        win: "Ctrl-Shift-J" 
    },
    exec: function(){ 
        formatJson() 
    },
    isAvailable: function(editor) {
        if (editor && editor.ace)
            return !editor.ace.selection.isEmpty();
        return false;
    }
}, plugin);

Of special interest are the bindKeys, which are the key combos that can be used to trigger this command. Users can change these defaults in the key bindings editor in the preference panel.

The isAvailable() function determines context for the command. In this case, the formatjson command is only available when an ace editor is focussed and there is code selected.