Sublime Text 2, also known as TextMate 2 in my office, is becoming increasingly useful to me. The only trouble thus far is the lack of community involvement to the extent of TextMate. That’s to be expected, it’s an alpha that went cross platform two seconds ago.
All that aside though, it’s an impressive alpha. I seriously love it’s potential and I’m really hoping to make it my own over the next few months. Naturally, the biggest hangup is the lack of tools I’ve become so accustomed to in TextMate. The great thing there is that the snippets and color schemes can be carbon copied into Sublime Text 2, the only outstanding items are Commands. Those aren’t directly portable from TextMate to Sublime Text 2.
I work in HTML, CSS, JavaScript, and PHP. In TextMate I make heavy use of the validators because it’s not an IDE and doesn’t have inline validation as you would find in an integrated environment. Sublime Text 2 doesn’t come with any such validators out of the box, but there are some forum posts. I found some resources on getting JavaScript validation working in Sublime Text 2:
- Sublime Text 2 (Alpha) Google Closure JavaScript Linter Build package
- (V2 only) JavaScript Google Closure Linter Build
I went through the process of getting Google Closure Linter installed and running fine via Terminal, but got [Errno 2] File not found
errors in Sublime Text 2. Additional Googling to find out why got me nowhere so I went with JSLint for the time being and thought I’d share how.
Adding JSLint validation to Sublime Text 2
To implement JSLint we’re going to first need JSLint. There are a number of resources for this, but I went with an OS X executable provided courtesy of javascriptlint.com. I made a new folder in my Applications folder and called it a day. JSLint is prepped and ready to go.
The other half of this puzzle is integration with Sublime Text 2. To do that, you’ll need to create a new Build System:
Upon invoking that menu, Sublime will give you an empty file to work with, outlining the structure of Build Systems. Sweet, but what to do with that? We want to send the current file we’re working with to JSLint for validation. To do that, our Build System will look like this:
{
"cmd": ["/Applications/jsl/jsl", "-process", "$file"],
"selector": "source.js"
}
Note the path to Applications/jsl/jsl
— that’s where I store my JSLint install, but you will need to modify that to the path on your system. The next parameter is the -process
flag that JSLint uses to define the file to validate, and the final parameter is the $file
flag that will be replaced with the file path to the file you’re currently working on.
The selector will help Sublime Text 2 automatically use this JSLint Build System when you’re working within a JavaScript file.
After modifying for your system, save the file to the default path Sublime Text 2 brings up in the save dialog and restart Sublime. Open up a .js file and hit CMD+B, or use Tools > Build. An inline prompt will appear giving you the JSLint results:
I’ll continue fiddling around with getting Closure Linter to work as I’d like to have the option, but if anyone’s got a tip regarding the error I was getting I’d love to hear some feedback.
Comments
Looks good Jon. I’d add in a file_regex to be able to jump to lines after validating. Here’s the code for that:
“file_regex”: “(.*?)\\(([0-9]+?)\\)”
I setup a repo on github which should work for windows and osx users. It makes it easy to download and drop into the JavaScript package:
https://github.com/n00ge/Sublime-Text-2-JSLint-Build
I’ve been looking for this. Thanks a lot.
Nice addition @chad
Thank you for the post!
A cross-platform alternative using Rhino for Sublime Text Editor 2 is available on https://github.com/eduardolundgren/sublime-jslint
Here is how I installed on OS X using Josh’s plugin.
cd ~/
git clone https://github.com/n00ge/Sublime-Text-2-JSLint-Build.git
cp -r ~/Sublime-Text-2-JSLint-Build/jsl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/JavaScript/jsl
cp ~/Sublime-Text-2-JSLint-Build/JavaScript\ \(OSX\).sublime-build ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/JavaScript/JavaScript.sublime-build
chmod +x ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/JavaScript/jsl/osx/jsl
Another option is the SublimeLint plugin (https://github.com/Kronuz/SublimeLint) which has support for python, php, perl, ruby, and js (via jshint).
cheers dude, useful find.
Just for note, JSLint (http://www.jslint.com/) is by Douglas Crawford, Javascript Lint (http://www.javascriptlint.com/) is a separate SourceForge Project.
They are not the same thing.