PSR-2 Coding Standard – Automatically Format in Sublime Text

It is always really nice when a code editor allows fixing your code to PSR2 coding standard by just hitting a single command.

At my work, we heavily use Laravel framework, which follows the PSR-2 coding standard. So, every day we create new packages, add new classes we first format our code to PSR-2 coding standard before submitting a pull request. We always strive to follow the coding standard and modern best practices to keep our code cleaner and elegant.


I personally use sublime as my primary code editor all the time, the main reason is its lightness and popularity among the developers around the world, another beneficial part is the huge availability of open-source packages for it.

Below, I'm going to cover how we format our PHP code to PSR-2 coding standard in sublime text. The team at symfony and the contributors have done really great job building a package that can be used with sublime text to format coding standard.

Step 1.

Installing php-cs-fixer via composer to run globally on your machine.

  composer global require friendsofphp/php-cs-fixer

After the installation process is completed open your terminal and simply run php-cs-fixer command just to confirm it's installed properly.

It is always a tedious job when you go and fix the formatting on each file you have in your project. Below, I have an example for you if you want to try to format your files manually.

 php-cs-fixer fix /path/project --rules=@PSR2

Please refer to the official doc to discover all the available rules and commands you could use for your formatting.


Step 2.

To automate the code formatting process, we're going to set up a build process in a sublime text, so whenever you need to format your code instead of running the long command, you could just press the button on a keyword to fix it.

Open your sublime text editor.
Go to Tools Menu > Build System > New Build System

    "shell_cmd": "php-cs-fixer fix $file --rules=@PSR2"

Now, try to save, a prompt window will open, create a new folder called Builds at that position, save it inside that folder with name PSR-2.sublime-build

The build process setup is now ready to use, before using it we need to make a selection under Tools Menu > Build System > PSR 2 which is a new command we just added with the process above.


  • If you're Windows, Linux user just hit Ctrl + B
  • If you're Mac user ⌘ + B

Learn more about PSR-2 Coding Standard.


Thanks for reading the article up to the end, if you are happy reading it and was helpful to you, feel free to share and leave your feedback below in the comment section.

Happy Coding!