Config-ease with Sublime Text Snippets

Reading Time: 3 minutes

I love when tools make my life easier. A conversation came up online the other night and I had shown someone a quick summary of the awesome power of Sublime Text. They wanted to know how I made the magic happen in that video. I felt I should and share it with everyone via a blog post. Here’s a quick video of my uses along with a description of what you can do with it, as well as how to make it work.

Simply put, Sublime Text has a feature called snippets. With snippets you can basically leverage a form of text expansion across multiple lines. And even place multiple blinking cursors. For me, I use it both ways. I have configuration snippets that simply put sections of configuration into the document. I do these for all the major components I run into often. For example, I have a snippet for the AAA config, another for the EIGRP config (including key chains etc), as well as one for SIP in the case the router may be a SIP gateway of sorts. I can take a project, open Sublime Text, and within a few short commands I can build out a modular configuration specific to the elements needed for that particular router installation.

When I leverage Sublime Text this way, I have to go back and scrub the configuration for changes such as source-interfaces, subnets, etc. However, I can quickly have the base configuration built.

The other way I like to leverage Sublime Text is to do repetitive tasks such as building a port channel across a couple of Nexus devices in a VPC. I like to keep VPC and Port Channel numbers the same, interface descriptions consistent, and not worry about typos in numbers. In this case, I have the snippet write the configuration of all two ports and the port channel interfaces. Once it writes it into the document, there are a series of cursor points (highlighting more than one at a time) that I can tab through. This means, On both interface channel-group commands, the port channel interface numbers, and the VPC numbers, I simply type 1234 once, and it enters it into all six areas.

Again, you can see these two use cases in my video at the beginning of the post.

The concept is pretty simple. You create a new text file that has a specific format, define a shortcut command and save it in a special folder. Now, when you are in Sublime Text you simply type your shortcut command and hit tab. Your configuration (or code) is expanded into that document. If you used cursor insertion points, it starts at the top most one.

First is an example of the just straight text expansion I use for quick configuration builds of multiple modules. Simply note where I placed the configuration code itself. Of particular interest is at the bottom of the file where I declare the tab completion shortcut. In this case confaaa.


Next we’ll look at the example I used cursor insertion points. The concept is the same as the above example with the exception that where I want cursor insertion, I place a $ followed by a number. Similar to declaring variables in other languages. The trick to these is quite simple. Anywhere I place $0 a cursor will be blinking in that spot. I simply type once, and it fills what I type into all those $0 spots. Continue this format with new numbers for each grouping of insertion points.

So now, where do you save these darn files and what do we call them? In Windows, these files get placed under c:\users\YOURNAME\appdata\roaming\sublime text 3\packages\users Change Sublime Text 3 to whatever version you are working with. I can confirm it works with 2 and 3.

On OS X they go in the /Users/mattouellette/Library/Application Support/Sublime Text 3/Packages/ folder.

In both cases the files are named YOURFILENAME.sublime-snippet

It’s really that simple!

Share this article:

Permanent link to this article: