Peter Haschke



Back to the Index

Sublime Text 2 Snippets

Although, I am nowhere near completely integrating Sublime Text 2 (my text editor of choice) into my workflow and mastering all of its bells and whistles, I thought I’d point to a feature I really like: Sublime Text Snippets.

The ability to create user snippets is quite useful when you want to include certain text or code fragments over and over again and are tired of repetitive typing of environment commands. A good example is the enumerate environment in LaTeX. It can be quite tedious to type \begin{enumerate} , \item , and \end{enumerate}  again and again. Luckily, there already exist shortcuts for the itemize and enumerate environment as part of the LaTeXTools package.[^1] Creating you own tab-completing fragments for other things that are not already implemented, fortunately, is really easy.

All you have to do is create a file with the ending/filetype .sublime-snippet and save it in your Sublime Text user directory (in my case that’s C:\Users\Peter\AppData\Roaming\Sublime Text 2\Packages\User ). Say, we wanted to create our own custom itemize environment fragment. We would do the following:

<snippet> <content><![CDATA[ ... ]]></content> <tabTrigger> ... </tabTrigger> <scope> ... </scope> <description> ... </description> </snippet>

In between the <content>   tags you would write your code fragment. So for example:

\begin{itemize} \item \item \item \end{itemize}

In between the <tabTrigger> tags you would write the term you want to use to trigger the environment. For example: bullets.

Finally you could specify the scope of the the snippet in between the <scope> tags. If you were writing a snippet for use in R which you wouldn’t want to accidentally trigger in LaTeX you would set this to: source.r . Your snippet would then only work if you were writing in a document with the .R   extension. Since we are writing a snippet for use with LaTeX, we would set this option to text.tex .

Another nice feature of the snippet capabilities is control over cursor placement after tab completion. In the example above we would write bullets  in our LaTeX documment, press tab  and Sublime Text would insert the itemize environment. If we were to press tab  again the cursor would move past the inserted environment. If we wanted to tell Sublime Text where to put the cursor we would use the following tags:

Finally you can add a description inside of the <description>  tags. This will show up in the snippet menu in the tool bar under Tools - Snippets.

Below is an example snippet for the figure environment. Download the Latex-Figure Snippet, place it into your user directory. Open a LaTeX document. Type fig  and tab away.[^2]

<snippet> <content><![CDATA[ \begin{figure}[!ht] \begin{center} \caption{${1}} \label{${2}} \includegraphics[scale=${3}]{E:/${4}.pdf} \parbox{\widthof{\includegraphics[scale=${3}]{E:/${4}.pdf}}} {\vspace{0.15cm}\scriptsize{${5}}} \end{center} \end{figure} ${0} ]]></content> <tabTrigger>fig</tabTrigger> <scope>text.tex</scope> <description>LaTeX Figure</description> </snippet>


[^1]: This is not the case for the tabular or table environments that are nicely implemented in WinEdt. I also use the snippets for my favorite ggplot geoms, etc. [^2]: Note the multi-cursor sweetness.

This post is filed under category Workflow, and contains the following tags: Sublime Text 2, LaTeX, code, SLT2.

Back to the Blog-Index