The following snippet is what I use in PHP when I am building out a class and need to quickly and easily flesh out the getters and setters for my protected and private variables:

To use this snippet yourself, throw it in a text file named getter-setter.sublime-snippet and put it in the folder where your user preferences are stored. For reference, on OSX the default location should be ~/Library/Application Support/Sublime Text 3/Packages/User/.

Once the snippet is saved, you can trigger it by typing //gs and then pressing the tab key. The snippet will replace the //gs tab trigger and you can begin to type the name of your variable, which will fill in in several spots. The snippet above expects that you will provide a snake-cased variable name, which it will convert into strong-cased versions for the actual getter and setter function names. For example, if your variable name is hello_world, the getter and setter function names will be getHelloWorld and setHelloWorld respectively.

Once the variable name is typed in, hit tab to move the cursor to the end of the code block and continue on your way.

What Is A Snippet

If you are using Sublime Text (or TextMate for that matter), and you are not making liberal use of snippets, you may be missing out on some nice time-savers. Sometimes it is possible to take large blocks of code and template-ize them to make them easily reusable with a few keystrokes. The ability to make sections of content within the snippet variable allows for even greater flexibility.

A snippet is a text file that defines, at a minimum, a block of text that can be inserted into a document. It takes the form of a very small XML tree.

Here is what a blank, auto-generated snippet will look like in Sublime:

As you can see, there are essentially 3 nodes to the file: scope; tabTrigger; and content. We will go through them in that order.

scope

Scope defines the type of file in which the snippet should be made available. A scope of source.python means that the snippet will only be made available in python files. Omitting the scope will make the snippet available in all scopes.

For a fairly comprehensive list of default scopes, check out this gist from iambibhas: https://gist.github.com/iambibhas/4705378.

tabTrigger

Snippets in Sublime Text can be programmatically added to content using the insert_snippet command through python. This is great if you are developing a plugin, but to the average user it is not exactly ideal. For the rest of us, the tabTrigger node allows you to specify a string that will act as your trigger alias. What this means is that you may type out the exact text of your tabTrigger, and while the cursor is positioned immediately after the given string, hit the tab key to replace your trigger text with the content of your snippet.

For example, if my tabTrigger is hello, I can hit the tab key while the cursor is positioned immediately after the o, and hello will be replaced with the content of my snippet.

To prevent accidental triggers, make sure to use a string for your tabTrigger that would be highly unlikely to type unless you intended to trigger your snippet.

 content

The content node is used to define what text will be inserted into your document when the snippet is triggered, either through tab-triggering or programmatic insertion.

There are many things you can do inside the content of a snippet, but I will cover just some of the highlights.

Wrap With CDATA

The content of all snippets must begin with <![CDATA[ and end with ]]>. The CDATA structure is an XML convention and it means character data. It is mostly unrelated, but if you want to know more about CDATA, the Wikipedia page is pretty good: https://en.wikipedia.org/wiki/CDATA.

Do Not Worry About Indentation

Snippets will inherit their base indentation from where your cursor is placed at the time of tab-triggering. If you want your snippet code to be indented 4 spaces (1 tab), you do not need to add those spaces to each line of your snippet content. Instead, make sure you trigger the snippet from a line that is already spaced to the first tabstop.

Simple Fillable Variables

You can add a variable to a snippet by using a dollar sign, followed by an integer. Your variables should start at 1 and increase as you define new ones. So begin with $1, then move to $2, and so on.

The variables are essentially marking places in your snippet where your cursor will be placed, beginning with $1 and continuing from there. You can move to the next position by pressing tab, and move backwards with shift + tab. This allows you to tab-trigger the snippet and have the cursor placed automatically at a spot where you will need to fill in some variable data, then hit tab to move to the next spot.

There is a $0, but it is a special case. It is always the last place your cursor will end up as you tab through your list of placeholders.

Repeating Fillable Variables

If you have several spots where your snippet needs to reuse the same value from a previous fillable variable, just repeat the placeholder’s number. The cursor is placed simultaneously at all the places where the placeholder number is used, allowing the you to fill in the value only once but have it typed in multiple locations.

Default Values For Fillable Variables

You can denote in your placeholder a default value for a placeholder. The syntax is ${1:put-default-value-here}. What this tells Sublime is that the first placeholder should fill in with put-default-value-here if a new value is not supplied before you tab to the next placeholder.

More Advanced Topics

The real workhorse of snippets for me is the REGEX replace capability, but a complete discussion of that system would be long and complex. There are a few other features I skipped just to try and keep things fairly high level. If you want to explore snippets further, I highly suggest starting with the unofficial Sublime Text documentation available here: http://docs.sublimetext.info/en/latest/extensibility/snippets.html.