Sublime Text 2 Build System Scripts - CoffeeScript & Node

A really great feature of Sublime Text 2 is the ability to create your own build scripts. A build script is helpful when you are writing in a language that needs to be compiled or executed from terminal / command line. I personally use build scripts to compile the current file into CoffeeScript as well as run the current file in Node JS.

Watch the tutorial or continue reading to learn how to make Sublime Text 2 build scripts.

Creating a Build Script

To get started, go to Tools->Build System -> New Build System... and you will be presented with the boilerplate for a build script.

We have four lines to feed into our .sublime-build file:

* **cmd** is an array of commends that will be run.*
  • selector is an optional way to scope the build script to a specific filetype

  • path is where the command is to be executed. Because sublime executes it in the python console, we need to specify where on our system the compliler we want to use lies.

  • file_regex specifies where the error lines are and what to be returned.

For a complete list of all the variables available within a build script, check the variable reference.

CoffeeScript Build Script

So, to create our CoffeeScript build file, we simply fill in the following three lines. Keep in mind that with the Node.js coffeescript compiler we would normally execute something similar to this in the terminal:

coffee -c /path/to/file.coffee


{
    "cmd": ["coffee","-c", "$file"],
    "selector" : "source.coffee",
    "path" : "/usr/local/bin"
}

Save your file in your user packages folder and restart Sublime Text. On OSX its located at: ~/Library/Application Support/Sublime Text 2/Packages/User

Now every time we hit CMD + B our coffeescript file will be compiled with the build script. If you want to watch the file for changes after the first compile, simply switch the first line to "cmd": ["coffee","-wc", "$file"],. If anyone is using my <a href="https://github.com/wesbos/coffeescript-growl" target="_blank">CoffeeScript-Growl plugin</a>, change your first line to "cmd": ["coffee", "-r", "coffeescript-growl", "-wc", "$file"], to get growl notifications.

Node.js Files

Another use for Sublime Text build scripts is launching the nodejs file you are currently working on. Our build script now looks something like this:


{
    "cmd": ["node", "$file"],
    "selector" : "source.js",
    "path" : "/usr/local/bin"
}

Thats is!

Pretty simple, eh? There is a lot of room for expanding these builds and making them do a lot of the manual work for you. If you have anything to add, please post it in the comments and I'll be sure to add it to the post.

As always be sure to Follow me on Twitter

facebook share2

Find an issue with this post? Think you could clarify, update or add something?

All my posts are available to edit on Github. Any fix, little or small, is appreciated!

Edit on Github

Syntax Podcast

Hold on — I'm grabbin' the last one.

Listen Now →

@wesbos Instant Grams

Master Gatsby

Master Gatsby

Building modern websites is tough. Preloading, routing, compression, critical CSS, caching, scaling and bundlers all make for blazing fast websites, but extra development and tooling get in the way.

Gatsby is a React.js framework that does it all for you. This course will teach you how to build your websites and let Gatsby take care of all the Hard Stuff™.

MasterGatsby.com
I post videos on and code on

Wes Bos © 1999 — 2021