Sublime Text 2 and Codesniffer (with Drupal) on Mac OsX Lion.

Friday, February 24, 2012 - 14:25

You should work with coding standards in mind, it is a very good thing when you share you things with other developers. And there tools out there to help you with the standards, for me that is the Drupal Coding Standards.

Sublime Text 2 (ST) is a small litlle text editor, that I think is perfect when you do not need a big IDE like Netbeans (which is really great, but sometimes have to much to offer to be simple to handle ). ST is free for evalution (not free as in beer), and one license costs $59.

On daily basis I switch between Netbeans and Sublime Text depending on what I am working on, and what I feel like.

But even with a small handy editor you could want some powerfull tools, like CodeSniffer, to get your code better, and if you are working with Drupal, you should also install the Drupal Code Sniffer to add Drupal Coding Standards to CodeSniffer.

ST is pluggable like every good editor I know of, and we are going to create a new Build System to get the checking of the Drupal coding standards inside ST.

Install Pear and Code Sniffer.

Download Drupal Code Sniffer (prefably with git clone, so you could easy update it later, the standards evole, you know ;-)).

Place the folder drupalcs in a good place, for instance /usr/local/bin/

Add drupalcs to CodeSniffer standards:

Open up you terminal.

sudo ln -sv /path/to/drupalcs/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards

Now we need to find the path to phpcs (tha's CodeSniffer), we will use it in the next step.

which phpcs

I got /usr/local/pear/bin/phpcs

And now go to you favourite text editor and create a new build-file for ST, with adding this:

  "cmd": ["/path/to/your/phpcs", "--standard=Drupal", "--extensions=php,module,inc,install,test,profile,theme", "$file"],
   "file_regex": "^(.*):(.*):(.*):(.*)$"

(This was taken from - but I had to change the path)

Change /path/to/your/phpcs to your own path to phpcs.

Save the file as DrupalCS.sublime-build at your User folder inside Application support for Sublime Text 2, what in the terminal equals to this:
~/Library/Application Support/Sublime Text 2/Packages/User

For me that is /Users/mikkeschiren/Library/Application Support/Sublime Text 2/Packages/User

Restart ST and now, if everything works, you should have the option DrupalCS in Tools > Build System. Mark that, open a file and hit Cmd B (Or Tools -> Build)

Now you should get an ouput of the the CodeSniffer results in the bottom of the window - if you have a large file, it will take some time.