Configure PHPCS In Sublime Text

PHP code sniffer scripts can be found here. As explained on that page, there are two scripts – one that detects issues or violations of a defined coding standard and other that fixes those violations.

You can configure your code editor to use these scripts. In this post I’ll explain how to do this for Sublime Text.

The first step is to of course download those scripts.

Next install PHP Code Sniffer package in Sublime Text by Package Control. If you don’t have this, it can be installed by pressing CMD+SHIFT+P > searching Install Package Control.

Once installed, you can press CMD+SHIFT+P once again and search for Package Control: Install Package. Hit enter, search for phpcs and install it.

Finally add your settings to PHP Code Sniffer by going to Sublime Text > Preferences > Package Settings > PHP Code Sniffer > Settings - User.

What settings to add here? You can check the options available to you by opening Settings - Default.

Since you want Sublime to be able to use the scripts you downloaded before, just go ahead and add their paths as follows –

I’m not quite sure what the -n stands for but note that I’ve added the --standard as WordPress by following the WordPress Coding Standard.

Now each time I save my file, it’ll autodetect issues with it (keeping WP standard in mind).

Git Diff To Sublime Text

I use git diff command to check the difference (additions/deletions) on my current branch. That works just fine but I’d love to have this result in a text editor like Sublime.

This can be done by saving the file in .diff format. Example: git diff > foo.diff and it’ll create a foo.diff file for you. This can then be opened in Sublime Text 🙂

Creating An Alias For A Mac OS Terminal Command

Another busy day for a daily blog post. I’ll go with a short one today. It’s about creating an alias for running a terminal command.

For example, the terminal command open -a "Sublime Text can be run by a short keyword of our choice, say sub. It would also allow us to open a file directly by typing in sub filename.

This can be done by adding the following text in the .bash_profile file:

alias sub='open -a "Sublime Text"'

If .bash_profile file doesn’t exist, we can create one using touch command as follows:

touch ~/.bash_profile

We’ll also need to reload .bash_profile after making changes to it. It can be done by the source command as follows:

source ~/.bash_profile

Finally, entering sub will open Sublime Text and sub filename will open that file in the text editor.

This is a simple example, we can create other aliases too, but I hope you get the idea. Thanks for reading! 🙂

Edit: This may not work if you’re on a different shell like ZSH. To check what shell you are on, run echo $SHELL command. If it returns /bin/zsh, you can create a .zshrc instead of the .bash_profile file (and rest remains the same).

Prettify Your Code

Today is busy day for me and so I’ll write a short (but useful) post. It’s about prettifying your code. Prettifying is basically formatting your code and making it neat and tidy. It can be a task if you do it manually but you don’t have to. Modern text-editors allow you to do this automatically.

If you use a text-editor, there’s a chance that it allows you to install third-party extensions.

I personally use Sublime Text. To install any extension (or package) in Sublime, you need to hit CMD+SHIFT+P and enter “install” in the search bar.

It’ll give you some options, out of which you need to choose “Package Control: Install Package”.

After you choose that, just type in “pretty” and you’ll get some suggestions. You can use any of those, depending on what language you use to code.

I use a couple of packages but my default (and favorite) one is HTML-CSS-JS Prettify. This package, once installed, will allow you to prettify your code by selecting it and hitting the CMD+SHIFT+H shortcut on the keyboard.

Other text-editors also offer this feature (e.g. Atom). Note: This is actually common knowledge but I really wanted to get a post out today 🙂