This Help topic describes how to write a short script that will allow you to assign a keyboard shortcut to a snippet.
Introduction
One of the most popular feature requests for the Aptana IDE over the past year has been for a way to assign a keyboard shortcut to a snippet. For example, you could type Ctrl+Shift+B and wrap the selected text in a <b> HTML tag set.
Although the Aptana IDE does not currently have a direct way to assign a keyboard shortcut to a snippet, you can write/modify a short Eclipse Monkey script that will assign the hotkey for you. The instructions below give an example script and explain how to set up the script to automatically execute a snippet via a hotkey combination.
Instructions
To assign a hotkey shortcut to a snippet:
- Create a new, empty Eclipse Monkey script. See Creating a new Eclipse Monkey script for instructions.
Name the file something related to the actual snippet that you want to execute (e.g. insert_form.js).
- Copy and paste the JavaScript below into your new file:
/* * Use this as a template to assign a key command to a snippet. Create a scripts directory in your * project, copy this into a new JavaScript file, change the menu name to what you like, and assign * a key command, using the guidance shown here: * http://www.aptana.com/docs/index.php/Adding_metadata_to_an_Eclipse_Monkey_script#Key_metadata * * Note that M1 is Control/Command, M2 is Shift, and M3 is Alt/Option * * Key: M1+M2+M3+F * Menu: Samples > Execute Snippet * Kudos: Ingo Muschenetz (Aptana, Inc.) * License: EPL 1.0 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript */ function main() { // Change these two to match the snippet you wish to find/use var snippetCategory = "HTML"; var snippetName = "Insert <form>"; var sourceEditor = editors.activeEditor; // make sure we have an editor if (sourceEditor === undefined) { showError("No active editor"); } else { loadBundle("com.aptana.ide.snippets"); var snippetManager = Packages.com.aptana.ide.snippets.SnippetsManager.getInstance(); var snippets = snippetManager.getSnippetsByCategory(snippetCategory); var snippet = null; for (i = 0; i < snippets.length; i++) { snippet = snippets[i]; if (snippet.getName() == snippetName) { break; } } if (snippet != null) { var range = sourceEditor.selectionRange; var offset = range.startingOffset; var deleteLength = range.endingOffset - range.startingOffset; var source = sourceEditor.source; var selection = source.substring(range.startingOffset, range.endingOffset); var content = snippet.getExpandedContent(selection); // apply edit and reveal in editor sourceEditor.applyEdit(offset, deleteLength, content); sourceEditor.selectAndReveal(offset, content.length); } } }
- Read the section on assigning keyboard shortcuts to an Eclipse Monkey script using key metadata: Adding_metadata_to_an_Eclipse_Monkey_script#Key_metadata
- Modify the script above to execute the snippet that you want to execute. (i.e. Change the value of "snippetName" to the snippet that you want this script to execute.)
- At the Key: keyword in the metadata, type the hotkey command that you want to assign to the script.
- Save your script.
- In the Scripts View, click the Refresh button to refresh your scripting environment.
- Use your new hotkey shortcut to test your script. Your snippet should now execute via the hotkey shortcut that you assigned to the script.