Komodo Abbreviations: Inserting Snippets by Name

Coding is fun, but typing is work. Komodo IDE provides several features that let you do less typing so you can have more fun.

The most obvious keystroke-saving feature is autocomplete, but Komodo also has a method for inserting snippets of code by typing their name in the buffer. We call this feature Abbreviations.

I've made a screencast showing abbreviations at work. This post gives some of the background, and covers some features that didn't fit in the video.

Why snippets are useful

Sometimes the autocompletion module can't resolve names in your code, especially when you're working with a dynamic framework like Rails or Django, or a UI toolkit like wx, where all the methods are in C libraries. Komodo lets you build a CIX file, an XML representation of the interface you're coding against, but creating CIX files isn't a trivial task. If you're going to be working with a framework for a short amount of time, and can't interest others in helping build the CIX file, you're likely looking at getting more ROI by whipping up a set of abbreviations.

Abbreviations are a special kind of snippet - you can see some samples in the Toolbox. In a nutshell, a snippet inserts a block of text at the cursor position in the current buffer. Snippets have more power than just inserting plain text; there are a number of shortcuts that can be inserted, including:

  • parts of the current file or project's name
  • paths for common interpreters
  • the current date
  • access to preference settings

You can also perform operations on a selection, or prompt for input at insertion time.

Tabstops: real-time buffer variables

Komodo: tabstops in a snippet

Additionally, a snippet can contain tabstops. These are placeholders for text to be supplied at snippet-insertion time. Each tabstop is numbered, and can appear multiple times, so when you supply text for tabstop #1, for example, all instances of tabstop 1 in the inserted block get replaced. You can find a good example of this in the samples that ship with the Toolbox, at [Samples > Abbreviations > PHP > class ].

Open a new buffer in Komodo (it doesn't have to be PHP, plain text will do) and double-click on the "class" snippet in the PHP folder. You'll notice that some of the text is highlighted, and some of the instances of "class" are selected. If you type "MyClass", you'll notice that both instances of the selected "class" are updated as you type. When you're done, you type a Tab key to move to the next tabstop (which is where they get the name).

The next step is accessing your snippets. Given that you're interested in snippets because you don't want to waste time typing, you're probably not interested in leaving the keyboard every time you want to insert a snippet. While you can use Ctrl-Shift-K and then type the name of the snippet to move to it, and then press Return to insert it, this takes your eyes away from the text you're typing. You don't want distractions like these while you're programming. Additionally, if you want the snippet called "class", the Invoke Tool dialog will show you all matches, not just the ones for the current language.

This is where turning your snippets into abbreviations pays off. They work very simply:

When you press Ctrl-T, Komodo grabs the word to the left of the cursor, and then looks for a snippet with that name inside a Toolbox folder-tree rooted at Abbreviations, followed by a sub-folder with either the same name as the language you're editing, or "General". For example, if you type "class" followed by Ctrl-T, and you're editing a PHP file, Komodo will insert the PHP snippet, and won't bother showing you the other snippets named "class".

Structuring Abbreviations

Once you start using abbreviations, you'll most likely end up with two kinds. The first you'll use whenever you're using a particular language; the second will consist of abbreviations specific to one project. You can keep them around for purposes of nostalgia, but there's a better way to do it.

Suppose that you're using Rails on two different projects. While Komodo does have a Rails CIX file, you notice that there are some abbreviations you'd like for Rails in general, and others are specific to two separate projects, which we'll call Flickrrrr.com and Hamsters.com. I might want to add a simple message abbreviation called "oog" to the Hamsters.com folder, with the expansion flash[:error] = "Sorry, we're out of gerbils". However, I might find that I'm writing a lot of attachment-handling code for the Flickrrrr.com web site, and I add a snippet called "hasatt" with this expansion:

has_attachment(:storage => :file_system,
               :resize_to => [%tabstop:'640x480'],
               :thumbnails => {:thumb => [%tabstop:'160x120'],
                               :tiny => [%tabstop:'50>']},
               :max_size => [%tabstop:1.megabytes],
               :content_type => :image,
               :processor => "Rmagick"

...to the Flickrrrr.com folder. One thing to note: all Komodo cares about is the language that you're editing, and the location of a possible abbreviation. So when you're done with the Hamsters project, you can move the folder to another part of the toolbox, or export it as a zip file, for later access.

Project-specific abbreviations

But we can do even better than that, by keeping project-specific abbreviations with their projects. When Komodo looks for an abbreviation, it actually looks in the Abbreviation folder of every opened project's toolbox, as well as the shared and standard toolboxes. This way, you could have your hamster-specific abbreviations available only when you're working on that project. When the project is out of the way, so are its snippets.


Autocomplete tends to work best in three situations:

  1. You're defining your own classes and methods, writing them in the same dynamic language that will be consuming them. Komodo usually picks these up as fast as they're entered.
  2. You're using the standard libraries written in the same dynamic language. If there's no meta-programming going on, Komodo can usually analyze these correctly as well.
  3. You're using libraries for which CIX files are available, or it's worth spending the time building them.

For the other cases, abbreviations are the way to go.

If you'd like to try out abbreviations for yourself in Komodo IDE, visit www.activestate.com/komodo-ide and download a 21-day trial today.