Sublime Text: Automating your tools

The snippet feature of Sublime Text allows you to avoid writing the same boilerplate code over and over again. For instance, on this blog, I write posts in MultiMarkdown and Jekyll compiles into HTML before posting.
This means that image links and code sections need to be appropriately flagged. That boilerplate is easy to make mistakes in so to avoid I wrote the following two snippets, with the help of this gist:

Snippet 1: R Code



<snippet>
    <content><![CDATA[<br>

---


<figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="o">$</span><span class="p">{</span><span class="m">1</span><span class="o">:</span><span class="n">code</span><span class="p">}</span></code></pre></figure>


---

<br>

$0]]></content>
    <tabTrigger>blog_r</tabTrigger>
    <scope>text.html.markdown.multimarkdown</scope>
    <description>R code snippet for personal blog</description>
</snippet>


This includes boilerplate formatting, auto-completion and exit points into the shortcut blog_r + TAB.



<snippet>
    <content><![CDATA[<br>
<img src="/assets/post_images/${1:file_path}" alt="0" width="600"/>
<br>
$0]]>
</content>
    <tabTrigger>blog_img</tabTrigger>
    <scope>text.html.markdown.multimarkdown</scope>
    <description>IMG Link for personal blog</description>
</snippet>


This again includes consistent spacing and a position for the relative file name. The shortcut for this snippet is blog_img + TAB and only applies to multimarkdown files.

Summary

Snippets need the .sublime-snippet extension to be recognized and need to be in the correct folder, but generating them through the Sublime Text interface handles the defaults gracefully.

The location is Tools>Developer>New Snippet...