Build Systems in Sublime Text 3

Sublime has highly customizable build systems that can add to your productivity if you learn how to use them to your advantage. You can define one for your project and whenever you are editing any file, you can run certain commands on the source file and see the output in the sublime console, without leaving the editor.

I mostly use IntelliJ for development but still find myself switching to sublime text time to time, depending upon the nature of the project. I mainly use sublime when I have to write some small script or a library, and when I use it I prefer to setup the build system to make it easier to test. In this post I am going to explain how to create one by creating an example build system for a hello-world php application. But the steps should be same for any language. So let’s get started.

The first thing that you are going to do is create a new build system. You can do that by going to below path

Tools > Build System > New Build System 

This will open a new file named untitiled.sublime-build. Update the file and put the below content

{
"cmd": ["php", "$file"],
"selector": "source.php",
"file_regex": "php$"
}

Now save this file with the name php.sublime-build. To give you some details about the file content;

  • cmd here means the command that we need to run with the arguments that we want to pass it
  • selector is an optional string used to locate the best builder to use for the current file scope. This is only relevant if Tools > Build System > Automatic is true
  • file_regex specifies the file pattern that our build is going to be working with.

After saving the file you can see the build system inside Tools > Build System. Now you can run any php file ending with php as specified above in the above snippet. Now let’s test our build system. Create a new php file and put the below content in it

<?php
echo “Hello world”;

Now let’s run this file that we have created. So select php from Tools > Build Systems and hit CMD + B if you are on Mac, CTRL + B if you are on windows or Linux. Once you run it, you will notice the output for the build in the console, as shown in the image below

In case you want to cancel a stuck build, you can do that by pressing CTRL + C if you are on Mac, or Ctrl + Break if you are on windows or linux.

You can use the same steps to create a build system for any language. For example, here is how the contents of the build file may look like for a Javascript application

{
"cmd": ["node", "$file"],
"selector": "source.js",
"file_regex": "js$"
}

Hope you enjoyed the article, you can learn more about the build systems in the Sublime Docs, if you have any comments or feedback, leave them down below and feel free to connect with me on twitter or say hi via email.