{"__v":2,"_id":"54d995416a09052100a6b6d7","category":{"__v":19,"_id":"54d5635632d98b0d00384afe","pages":["54d5635732d98b0d00384b12","54d5635732d98b0d00384b13","54d5635732d98b0d00384b14","54d847c125e90a0d00db551f","54d995416a09052100a6b6d7","54d995534c49f40d00dff98a","54d9956b4c49f40d00dff98c","54dbce210e553a0d00dc1d09","54dcdf7bac05560d00335a15","54dd133c08f6b61700a691f7","54dd136108f6b61700a691fa","54dd27e0db3b362100131294","54dd2838ca1e5219007e9dcb","54dd28c8db3b362100131296","54deb1133a91410d001b1791","54deb134c4a3570d009d4a60","54dec5c0c2b4b70d009c3f05","54ded495e8e35a0d007551a0","54ded57d8e7b000d007c7f31","551447a90edb420d0050016e","551c4369ff892b3700d1c616"],"project":"54d53c7b23010a0d001aca0c","version":"54d5635532d98b0d00384afb","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-06T23:44:18.708Z","from_sync":false,"order":4,"slug":"api-plugins","title":"API Plugins"},"parentDoc":null,"project":"54d53c7b23010a0d001aca0c","user":"54cfa8e1a8a4fd0d00b7fd1d","version":{"__v":10,"_id":"54d5635532d98b0d00384afb","forked_from":"54d53c7c23010a0d001aca0f","project":"54d53c7b23010a0d001aca0c","createdAt":"2015-02-07T00:59:01.934Z","releaseDate":"2015-02-07T00:59:01.934Z","categories":["54d5635632d98b0d00384afc","54d5635632d98b0d00384afd","54d5635632d98b0d00384afe","54d5635632d98b0d00384aff","54d5635632d98b0d00384b00","54d5635632d98b0d00384b01","54d5635632d98b0d00384b02","54d652097e05890d006f153e","54dd1315ca1e5219007e9daa","54e21e2b22de1c230094b147","54e68e62a43fe13500db3879","54fa1d3fe7a0ba2f00306309","551c453a23a1ee190034d19a","551df586e52a0b23000c62b6","551f39be6886f8230055f02a","55a6720751457325000e4d97"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":["5508b93b775d2c1700db0a40"],"next":{"pages":[],"description":""},"createdAt":"2015-02-10T05:21:05.503Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"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.\n\nThe following example shows how to create a very basic command. \n\n```\ncommands.addCommand({\n    name: \"oneplusone\",\n    exec: function(){ \n        console.log(1 + 1);\n    },\n}, plugin);\n```\n\nThe command is called `oneplusone` and when [triggered](https://docs.c9.io/api/#!/api/commands-method-exec) the exec function is called, printing `2` in the browser's console. Here's a short code snippet that triggers this command:\n\n```\ncommands.exec(\"oneplusone\");\n```\n\nThe next example is more detailed and specifies a lot more details about the command. This specific example is used in the [format json tutorial](http://cloud9-sdk.readme.io/v0.1/docs/create-your-first-package);\n\n```\ncommands.addCommand({\n    name: \"formatjson\",\n    group: \"Format\",\n    bindKey: { \n        mac: \"Shift-Command-J\", \n        win: \"Ctrl-Shift-J\" \n    },\n    exec: function(){ \n        formatJson() \n    },\n    isAvailable: function(editor) {\n        if (editor && editor.ace)\n            return !editor.ace.selection.isEmpty();\n        return false;\n    }\n}, plugin);\n```\n\nOf 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.\n\nThe 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.","excerpt":"","slug":"commands","type":"basic","title":"Commands"}
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](https://docs.c9.io/api/#!/api/commands-method-exec) 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](http://cloud9-sdk.readme.io/v0.1/docs/create-your-first-package); ``` 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.