Sharing code snippets with your team in Sublime Text

For the last couple of projects at the office, we’ve been working with multiple developers on the same site (where as before that we’d each work on our own separate project). One of the biggest upsides to this is that our workflows for doing certain things in code, in the cms (Expression Engine in our case) and in the templates are being fine tuned all the time and we’re taking the chanc to experiment with things and iterating over them quickly.

When one of those experiments sticks and turns out to be a good idea, we keep it in our workflow.

One things we’ve been doing to facilitate this is sharing code snippets amongst each other. And for the people using Sublime Text, I’ve got a simple trick to do that for you today.

Quick example of what one of those snippets might look like.
Here we’re using CE Image to display an imagec from an entry and have set a couple of default values.

<snippet>
<content><![CDATA[
{exp:ce_img:pair src="{${1:image:url}}" max_width="${2:300}" max_height="${3:300}" save_type="${4:jpeg}" quality="${5:80}"}
<img src="{made}" width="{width}" height="{height}" alt="{title}" />
{/exp:ce_img:pair}
]]></content>
<tabTrigger>ceimg</tabTrigger>
</snippet>

With this in my snippets folder, i can type ceimg + tab and ST will replace that with the snippet. Then I can tab through the arguments and change them where & when that’s needed.

Great, now how do we get this to the other members of our team and get them to add theirs? Easy, we use git.
Sublime Text stores these snippets in ~/Library/Application Support/Sublime Text 3/Packages/User/. There you make folder (eg: ‘ee-snippets’) and add our snippets to that. Then you add a git repository in that folder and push to your prefered git hosting service (bitbucket.com has free & private repositories if you just want to share this with your team and not with the world).

Whenever you add or change a snippet, you commit it and push it to the repo so the other developers on your team can pull down those changes and use the snippets on their own machine.

Advertisements