Sublime Text is currently the text editor of choice for a number of developers in the open-source community. It’s sophisticated, has powerful text selection and customization support and also includes a feature not used by many – its build system. In this post, I’d like to take you through the Sublime build system and share build scripts for working with many of the languages and tools we use today.

These will include scripts for Grunt, CoffeeScript, SASS and others.

Introduction

Sublime Text build systems can be considered simplistic, but highly customizable. The basic idea is that each type of Build profile is powered by a “.sublime-build” file – a JSON representations of the commands, paths and configuration needed to build a project using a specific tool or set of tools.

Builds can be executed using a keyboard shortcut (Command+B on Mac is the default on Mac or F7 on Windows), via the Tools menu or when a file is saved. If a project is currently open, the build system we last selected (e.g grunt) will be remembered.

When Sublime is passed references to external tools/binaries via a “.sublime-build” files, it can execute these applications with any arguments or flags that may be necessary. It is also able to pipe back the output of calling any of these apps using the built-in console in Sublime. Effectively this allows us to easily build projects without the need to leave our editor.

Adding a custom Build System

Sublime populates its Tools/Build System menu based on the “.sublime-build” files stored in the Sublime “Packages” directory. Should one need to locate this, it can be found in “~/Library/Application Support/Sublime Text 2/Packages/User” (if using OS X) or the corresponding Packages/User directory on other platforms.

A basic “.sublime-build” file could be represented in key/value form as follows:

{"cmd": ["command", "argument", "--flag"],"selector": ["source.js"],"path": "/usr/local/bin","working_dir": "/projects/"
}

Keys supported include:

  • cmd - An array containing a command to run and its desired arguments and flags. Note that Sublime will search your PATH for any tools listed unless an absolute path has been used to point to them.
  • selector – 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.
  • path – An optional string that replaces your current process’s PATH before calling the commands listed.
  • working_dir – An optional string defining a directory to switch the current directory to prior to calling any commands.
  • shell - An optional boolean that defines whether commands should be run through the shell (e.g bash).
  • file_regex – An optional regular expression used to capture error output from commands.

For a comprehensive list of keys supported in Sublime build scripts, see theunofficial docs.

Build Variables:

In addition, Sublime supports variable substitutions in build files such as$file_path (for the path to the current file) and more. These include:

  • $file_path – the directory of the current file being viewed
  • $file_name - only the name portion of the current file (extension included)
  • $file_base_name - the name portion of the current file (extension excluded)
  • $project_path - the directory path to the current project
  • $project_name – the name portion of the current project

A complete list of substitutions supported is also available.

Grouping build tasks

Some developers also like to group together tasks within an external bash script (or equivalent). For example, here’s a simple git-ftp deploy script you can use with Sublime to commit and push your latest changes with git and then upload your latest files to FTP.

Example: Commit, Push And Upload To FTP

deployment.sh:

#!/bin/bash
git add . && git commit -m 'deployment' && git push && git ftp init -u username  -p password - ftp://host.example.com/public_html

deployment.sublime-build:

{"cmd": ["deployment"],"working_dir": "${project_path:${folder}}"
}

If you haven’t used git-ftp before, Alex Fluger has a solid article about using it that may be of interest.

Targeting Platforms:

Sublime build files also support specifying configuration data for specific platforms (namely, OS X, Windows and Linux). Targeting a platform can easily be done by specifying another element in our config with the name of the platform. e.g

{"cmd": ......"windows":{"cmd":  ...},"osx":{"cmd": ...},"linux":{"cmd": ...}
}

Build files for popular front-end tools

To help you get started, I’ve written a collection of “.sublime-build” files for some of the front-end tools I’m aware web developers are using these days below.

Most of these will function fine without the need to specify path, but if you run into an issue with paths, try including it to your config (e.g "path": "/usr/local/bin").

grunt:

{"cmd": ["grunt", "--no-color"],"selector": ["source.js", "source.less", "source.json"]
}

Node Build Script:

{"cmd": ["h5bp", "--no-color"],"selector": ["source.js", "source.less", "source.json"]
}

CoffeeScript:

{"cmd": ["coffee","-c", "$file"],"selector" : "source.coffee"
}

SASS:

{"cmd": ["sass", "--watch", ".:."],"working_dir": "$file_path","selector": ["source.scss", "source.sass"]
}

Whilst a more verbose version with automatic minification and watch config could be written:

{"cmd": ["sass", "--watch", "sass:stylesheets", "--style", "compressed"],"working_dir": "$project_path","selector": ["source.scss", "source.sass"]
}

LESS:

{"cmd": ["lessc", "-x", "$file", "$file_path/$file_base_name.css", "--verbose"],"shell" : true,"selector": "source.css.less"
}

Stylus:

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

(a more comprehensive version of this can be found in the LESS-build-sublimeproject.)

Jade:

{"cmd": ["cmd", "/c", "jade", "$file"],"selector": "source.jade"
}

r.js (RequireJS Optimizer):

{"cmd": ["node", "r.js", "-o", "app.build.js"],"working_dir": "$project_path","selector": "source.js"
}

UglifyJS:

{"cmd": [ "node", "uglifyjs", "-o", "${file_path}/${file_base_name}.min.js", "$file"],"selector": "source.js"
}

Node (just passing in directly):

{"cmd": ["node", "$file"],"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)","selector": "source.js"
}

Pandoc (Markdown to HTML):

{"cmd": ["pandoc", "-S", "-s", "-f", "markdown", "-t", "html", "-o", "$file_base_name.html", "$file"],"selector": "text.html.markdown"
}

(and when it’s released, Yeoman):

{"cmd": ["yeoman", "build", "--no-color"],"selector": ["source.js", "source.scss", "source.sass", "source.html"]
}

JSHint:

I imagine most web developers would want to run JSHint from within a broader build process, but if you’d also like to run it standalone via a Sublime build file, thesublime-jshint package has a build file that will work fine on both OS X and Windows.

Build files for specific programming languages

I also thought that while we were looking at build files, it would be useful to demonstrate how these can be used to build/compile with some popular programming languages. These may differ to those included with Sublime by default, but are useful for reference:

Ruby (using RVM):

{"cmd": ["~/.rvm/bin/rvm-auto-ruby", "$file"],"file_regex": "^(...*?):([0-9]*):?([0-9]*)","selector": "source.ruby"
}

Python:

{"cmd": ["python", "-u", "$file"],"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)","selector": "source.python"
}

PHP:

{"cmd": ["/usr/bin/php", "-l", "$file"], <- Couldn't just use "php" ?"file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)","selector": "source.php"
}

Java:

{"cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],"working_dir": "${project_path:${folder}}","selector": "source.java","shell": true
}

.Net (Windows):

{"cmd": ["%WINDIR%\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild", "${project_base_name}.sln"],"shell": true,"working_dir": "${project_path:${folder}}"
}

C:

{"cmd": ["make && ./a.out"],"path": "/usr/bin:/usr/local/bin:...","shell": true
}

C++ (via g++):

(Note that we’re also able to specify OS-specific configurations too, as in the below):

{"cmd": ["g++", "$file", "-o", "$file_base_name", "-I/usr/local/include"],"selector": "source.c++","windows": {"cmd": ["cl", "/Fo${file_path}", "/O2", "$file"]}
}

Haskell:

{"cmd": ["runhaskell", "$file"],"file_regex": "^(...*?):([0-9]*):?([0-9]*)","selector": "source.haskell"
}

Conclusions

Sublime build systems are awesome and can help you avoid the need to manually switch between your editor and external build tools regularly. As you’ve hopefully now learned, putting together your own custom build systems is a straight-forward process and I’d recommend trying it out if Sublime happens to be your editor of choice.

Custom Sublime Text Build Systems For Popular Tools And Languages相关推荐

  1. Sublime Text Build 3176 安装图文详细教程

    1.著作权声明 1.1.本图文详细教程为[推优创意]原创教程,[推优创意]拥有著作权,未经本人许可,谢绝任何形式的全部或部分转载!违者必究! 1.2.所有文章在 微信公众号@推优创意.博客园@推优创意 ...

  2. Sublime Text Build 3126 x64的安装、注册和汉化

    安装Sublime Text Build 3126 x64 本文为大家介绍sublimeText3的安装.汉化及常用插件和常见的几个问题的解决方案. 1.   安装 1)官网下载 最新版Sublime ...

  3. Sublime Text C# 编译(csharp.sublime-build)

    制作: 1. 配置环境变量PATH C# 7.0 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0 ...

  4. 杂谈:用 Sublime Text 2 写 ActionScript3

    Sublime Text这是程序员最喜爱的编辑器,说说在win7下使用Sublime Text来编写as文件以及编译与运行swf. 准备工作 1.Sublime Text 22.Java 的JDK(j ...

  5. sublime Text 3配置Maple编译运行

    Maple简介 Maple是目前世界上最为通用的数学和工程计算软件之一,在数学和科学领域享有盛誉,有"数学家的软件"之称.Maple 在全球拥有数百万用户,被广泛地应用于科学.工程 ...

  6. sublime Text 些许使用配置

    在安装numpy等库函数时,通过"命令提示符"操作显示库函数已经安装完毕,在pycharm中可是依然显示引用失败,尝试使用sublime,显示可用,遂好好使用sublime,现配置 ...

  7. Sublime Text[崇高文本]----最性感的编辑器(程序员必备)

    代码编辑器或者文本编辑器,对于程序员来说,就像剑与战士一样,谁都想拥有一把可以随心驾驭且锋利无比的宝剑,而每一位程序员,同样会去追求最适合自己的强大.灵活的编辑器,相信你和我一样,都不会例外. 自打开 ...

  8. sublime text 3 , 3143

    SUBLIME TEXT 3 : 3143 September 2017 Update. All other keys were invalidated due to being shared. Cu ...

  9. linux sublime 命令行启动,命令行 – 使用命令行在Sublime Text 3中打开一个文件夹

    我试图打开一个在崇高的文本3的目录. 我可以使用subl命令从命令行启动升华. 帮助文本显示如下: Sublime Text build 3059 Usage: subl [arguments] [f ...

  10. Sublime Text 崇高文本 ----最性感的编辑器(程序员必备)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 代码编辑 ...

最新文章

  1. 揭秘阿里中台!一文看懂阿里推荐业务的两大利器
  2. PostGresSQL简介与Windows上的安装教程
  3. A Simple Problem with Integers
  4. windows和linux下的文件路径表示
  5. 《Python程序设计》——1.2 程序开发周期
  6. sourcetree删除文件夹、重新指向并重定义主分支、
  7. html5 职工入职后台管理系统_10个酷炫的后台模板
  8. Cesium:改变地球背景
  9. 选择、冒泡、插入、快速排序
  10. Android应用保活方案的另类出路,让你应用长生不老,实战案例
  11. Office2007 三合一绿色精简版
  12. 第一章 批判性思维概念
  13. ERwin Data Modeler数据库建模工具使用纪要
  14. html5语文答题制作,语文万能答题模板
  15. 云技术会颠覆IT平台吗?
  16. 如何锻炼提高自己的逻辑思维?这里给你7个方法!
  17. 最基础硬件学习 | 简单闪烁灯制作
  18. 亚马逊账号被关联能申诉得回来吗
  19. 【Xilinx】Spartan 7上手指南(ARTY S7开发板)
  20. 2022团队天梯赛答案解析

热门文章

  1. 2020-10-01
  2. 获取代理电脑的https证书方法
  3. Python操作IHTMLDocument2用于自动化测试
  4. 【九度OJ1348】|【剑指offer36】数组中的逆序对
  5. 蓝桥杯 大整数乘法 试题 算法训练 P0805
  6. QT4升级QT5调研报告
  7. 【SpringBoot_ANNOTATIONS】生命周期 04 BeanPostProcessor 后置处理器
  8. python安装成功之后教程_python安装教程 Pycharm安装详细教程
  9. react打包后图片丢失_给 React 组件自动加上 react-hot-loader
  10. 修改 oracle 数据库 TNSLSNR.exe 占用 8080 端口