Add or edit Code snippets and templates in Aptana Studio 3

I’ve searched now for a while, to edit and create some code snippets and code templates within Aptana Studio 3. These code snippets should help to make my work more efficient and faster, when the content assistent propose them while i’m typing. In Aptana Studio 2 we were able to manage all code snippets within the preferences window, but in Aptana Studio 3 isn’t it so. In Aptana Studio 3 we must integrate our code snippets and templates within bundle.rb files.

Attention: Here is an update available for this post! 😉

At first, a few basic knowledge about the code snippets. The code snippets consists of three parts, named the trigger, expansion and sometimes the scope.
The trigger is the text that is needed to trigger the snippet. That means, when i’m during programming and type the first letters from the trigger, the code assistent propose me the code snippet. Further the expansion is the code snippet it self, that will replace the typed trigger text. The expansion can also contain several variables and tab stops. At least the scope defines in which file the trigger text should response.

Now, after all these informations i would start to create my own code snippets, but there’s direct the first problem:
Where in hell are these ‘bundle.rb’ and what structure have them?!

‘bundle.rb’ are, as the name is shown, Ruby files and can simplify edit with a text editor.
To find these bundle.rb files navigate into your Aptana Studio 3 installation directory, and search the files, as sample with:

find . -name bundle.rb

The result are some founded files within the ‘./configuration/org.eclipse.osgi/bundles/84/1/.cp/’ directory, that are in detail


Now, you can edit these files, while the respective directory name is containing the programming language name.
We want to do it now as a test for PHP, therefore we open the ‘./bundles/php.ruble/bundle.rb’ in a text editor like gedit:

gedit ./configuration/org.eclipse.osgi/bundles/84/1/.cp/bundles/php.ruble/bundle.rb

In these file you can also manage all menuentries that are placed within the mainmenu under ‘Commands’->’PHP’->’…’

The first sample is very easy. ‘First snippet’ is the name from our first snippet. These name is in Aptana Studio 3 displayed when the trigger is matched. The trigger and the expansion are as described above.

snippet "First snippet" do |snip|
  snip.trigger = "first"
  snip.expansion = "[first snippet]"

After finish the changes within the bundle.rb, you need to restart you Aptana Studio, so now the code snippet is available for using.
Open a PHP file, type the first letters from your snippet ‘fir’ and press ‘strg+space’.

If only one snippet is available, that is matched with the trigger text, the expansion text is automatically included. Are there matched multiple trigger text, it appears a menu with the matched code snippets. Select the needed one and press enter.

When you now add all your code snippets, have attention by using a $ or ” symbol within your expansion. If you need the $ or ” symbol as sample for a PHP variable, you must escape it, like:

snippet "Second snippet" do |snip|
  snip.trigger = "second"
  snip.expansion = "\\$phpVar = \"the value\";"

The second sample insert a variable assignment, in detail:

$phpVar = "the value";

Further the code snippets can also have include tab stopps. These tab stopps consists of two parts. The first part is the number of order, in which they are processed. The second part, separated by a :, is the default value.

snippet "Thrid snippet" do |snip|
  snip.trigger = "third"
  snip.expansion = "\\$${1:var_name} = \"${2:var_value}\";"

Is now the trigger is matched with the typed text, and you include the snippet, the default text of the tab stopp will be included and automatically selected by the text selection. Now you can direct override these text, or assume with the default text and jump with a ‘tab’-key press to the next code snippet tab stopp.

The next code snippet shows, that you are also able to create line breakes with in snippets.

snippet "Fourth snippet" do |snip|
  snip.trigger = "fourth"
  snip.expansion = "function ${1:method_name}(${2:attribute}) {\n}\n"

In the following snippet are using concatenation to create a multiline snippet for a todo comment, that is also execute a shell command to include the current date. To execute a shell command, you just need insert these command within back ticks.

snippet "Fifth snippet" do |snip|
  snip.trigger = "fifth"
  snip.expansion  = "/**\n"
  snip.expansion += " *\n"
  snip.expansion += " * @todo       ${1:do}\n"
  snip.expansion += " * @date       "+`date +%Y-%m-%d`
  snip.expansion += " *\n"
  snip.expansion += " */\n"

The last sample is shows the mirroring. Mirroring means, you include two same tab stopps that are synchron replaced:

snippet "Sixth snippet" do |snip|
  snip.trigger = "sixth"
  snip.expansion = "function ${1:method_name}(${2:attribute}) {\n"
  snip.expansion += "\t${2:attribute}\n"
  snip.expansion += "}\n"

Now you are able to edit many useful snippet that can help to save a lot of time.

Notice: If you have an error within your bundle.rb, the console view in Aptana will display them by starting up.

If you work within a group of developers, it’s useful to share all the collected snippet, so that every developer in the group can use it. For these you could mount the snippets folder from a cloud service like ubuntu one. When also every developer in the group must be able to change and add some snippets, you could include the snippet folder with a SVN repository. So every developer check out the snippet repository into the snippet folder, and is now able to make changes, or update the snippets.
How you can do this by using a SVN repsotiory, read it here soon… I will write an article about it!

These are all in all many useful things, but there are some questions available:

  • How can i change a snippet, without an Aptana Studio 3 restart? Without a restart the changed snippet isn’t direct available.
  • How can i use the scope on the best? It is possible to use a wildcard to select as sample all *class.php files?
  • How can i trigger the menu entries from the commands menu named ‘Declarations’ and ‘Globals’? The snippets from the ‘Return’ menu works by me, why the other ones not?

So many questions and more..
If you have any experience with code snippets, answers of my question, and so on.. Contact me or post a comment to complete this article.

Thank you guys 😉

    • Vince
    • June 29th, 2011

    What an incredibly convoluted way to have to add new templates. Just, wow. I’ve actually switched back to Aptana 2 because it’s far easier to use.

    • Ivan
    • March 26th, 2012

    I did not find elegance way how to apply changing in snippets, but if click Reload in Bundle View – changes in snippets.rb will apply w/o restart Studio

  1. Hello, Your article is really interesting and I immediately put into practice what is written. Now, the snippet file for PHP is created but Aptana 3 after the restart, It always shows 52 items of PHP code. as start. What’s wrong? Thanks in advance for the answer!